Created
May 18, 2022 20:44
-
-
Save jonz94/7172ca0a76e72676121b5afb78924b59 to your computer and use it in GitHub Desktop.
Clean up PSReadLine history file (with backup)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$historyPath = (Get-PSReadLineOption).HistorySavePath | |
$directory = (Get-Item $historyPath).DirectoryName | |
$basename = (Get-Item $historyPath).Basename | |
$extension = (Get-Item $historyPath).Extension | |
$timestamp = (Get-Date).ToString("yyyy-MM-ddTHH-mm-ssZ") | |
$backupPath = "$directory\$basename-$timestamp-backup$extension" | |
Copy-Item $historyPath $backupPath | |
$uniqueHistory = @() | |
$history = Get-Content $historyPath | |
[Array]::Reverse($history) | |
$history | Foreach-Object { | |
if (-Not $uniqueHistory.Contains($_)) { | |
$uniqueHistory += $_ | |
} | |
} | |
[Array]::Reverse($uniqueHistory) | |
Clear-Content $historyPath | |
$uniqueHistory | Out-File -Append $historyPath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment