Skip to content

Instantly share code, notes, and snippets.

@jonz94
Created May 18, 2022 20:44
Show Gist options
  • Save jonz94/7172ca0a76e72676121b5afb78924b59 to your computer and use it in GitHub Desktop.
Save jonz94/7172ca0a76e72676121b5afb78924b59 to your computer and use it in GitHub Desktop.
Clean up PSReadLine history file (with backup)
$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