Saving History to Script with PowerShell

by Sep 21, 2012

PowerShell is all about trial and error, and when you want to save your interactive input to a script file, this is how it could be done:

Get-History -Count $MaximumHistoryCount | ForEach-Object { $_.CommandLine | Out-File $env:temp\myscript.ps1 -Append }

Next, open the script in an editor of choice and edit the script, keeping only the lines that proved to be useful:

notepad $env:temp\myscript.ps1

Note that $MaximumHistoryCount by default is set to 64, so you can only get the latest 64 commands. Increase the history buffer:

$MaximumHistoryCount = (32KB -1)

You can place this line in your profile script to automatically expand the history buffer when PowerShell starts.

Twitter This Tip! ReTweet this Tip!