jeffharbert

Save Powershell Command History on Exit

Nov 28th, 2016
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Add this to your Powershell profile, which is a PS1 file located at "C:\Users\{username}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
  2. # Each time you exit Powershell, the commands you used will be saved in a PS1 file.
  3. # Change C:\path\to\file to wherever you want the PS1 file to be saved.
  4. # The Register-EngineEvent and Get-History lines must each be on a single line.
  5.  
  6. Register-EngineEvent PowerShell.Exiting {$timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
  7. Get-History | Foreach-Object { $_. CommandLine } > C:\path\to\file\$timestamp.ps1}
Advertisement
Add Comment
Please, Sign In to add comment