jeffharbert icon

Save Powershell Command History on Exit

jeffharbert | PRO | 11/28/16 12:42:33 PM UTC | 0 ⭐ | 481 👁️ | Never ⏰ | []
PowerShell |

570 B

|

None

|

0 👍

/

0 👎

# Add this to your Powershell profile, which is a PS1 file located at "C:\Users\{username}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
# Each time you exit Powershell, the commands you used will be saved in a PS1 file.
# Change C:\path\to\file to wherever you want the PS1 file to be saved.
# The Register-EngineEvent and Get-History lines must each be on a single line.
 
Register-EngineEvent PowerShell.Exiting {$timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
Get-History | Foreach-Object { $_. CommandLine } > C:\path\to\file\$timestamp.ps1}

Comments