Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Last active September 20, 2025 16:44
Show Gist options
  • Save tzkmx/1f54f9795a337d96712b88dfbde5de07 to your computer and use it in GitHub Desktop.
Save tzkmx/1f54f9795a337d96712b88dfbde5de07 to your computer and use it in GitHub Desktop.
PowerShell History on demand

PowerShell History Management Scripts

A set of PowerShell scripts to persist your command history in a single, searchable file and keep your history clean.

How It Works

This system has two main parts that work together: one that saves and cleans up your history when you close PowerShell, and another that lets you search and reuse that saved history.

On Session Exit: Saving and Cleaning

The Profile.ps1 script registers an action that automatically runs whenever you exit a PowerShell session. It performs two key cleanup tasks:

  1. Saves History: It takes all the commands from your session and appends them to a single file: session_history.csv. When you want to find an old command, you use the iah alias. This runs the Invoke-ArchivedHistory function from Import-History.ps1, which:

  2. Reads the master history from session_history.csv.

  3. Filters out "noise" by removing common, low-value commands like ls, dir, exit, and history before showing you the list.

This way, the list you choose from isn't cluttered, making it much easier to find the important commands you want to reuse.

Scripts

  1. Profile.ps1: Configures your environment on startup and sets up the automatic history saving/cleaning process for when you exit.

    Place the Import-History.ps1 script in your PowerShell scripts directory (e.g., C:\Users\YourUser\Scripts).

This project is licensed under the MIT License.

function Invoke-ArchivedHistory {
[CmdletBinding()]
param (
[string]$Pattern,
[string]$HistoryFilePath = "$env:USERPROFILE\Scripts\session_history.csv"
)
if (-not (Test-Path $HistoryFilePath)) {
Write-Error "History file not found: $HistoryFilePath"
return
}
# Import and clean the history
$history = Import-Csv -Path $HistoryFilePath |
Select-Object -ExpandProperty CommandLine -Unique |
Where-Object {
$_ -and # Not null or empty
$_ -notmatch '^\s*(exit|ls|dir|gemini|history)\s*$' -and # Exclude common noise
$_ -notmatch '^\$p = Get-CimInstance' # Exclude VSCode process queries
}
# Filter by pattern if provided
if ($Pattern) {
$history = $history | Where-Object { $_ -match $Pattern }
}
# Let the user select a command
$selectedCommand = $history | Out-GridView -PassThru -Title "Select a command to add to history"
if ($selectedCommand) {
# Add the selected command(s) to the current session's history buffer
# This makes it immediately available with the up-arrow.
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($selectedCommand)
Write-Host "Added to history. Press the up-arrow to access:"
Write-Host $selectedCommand -ForegroundColor Green
}
}
$scriptPath = "$env:USERPROFILE\Scripts"
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($currentPath -notlike "*$scriptPath*") {
[Environment]::SetEnvironmentVariable("PATH", "$currentPath;$scriptPath", "User")
Write-Host "✅ Added $scriptPath to user PATH."
} else {
Write-Host "ℹ️ $scriptPath is already in PATH."
}
. "$scriptPath\Import-History.ps1"
Set-Alias -Name iah -Value Invoke-ArchivedHistory
Register-EngineEvent -SourceIdentifier 'PowerShell.Exiting' -SupportEvent -Action {
# Your custom commands to run on exit go here
# For example, to export history to a file (it would serve auditing purposes):
$auditPath = $scriptPath = "$env:USERPROFILE\Scripts\session_history.csv"
# Get-History | Export-Clixml -Path $auditPath -Force
get-history | epcsv -append -path $auditPath
clear-history
rm -force (get-psreadlineoption).HistorySavePath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment