View The History of Your PowerShell Commands
This page explains that the Get-History, stores a cache of recent commands. Many aspects are best suited to the command-line, while a few techniques are also useful in the ISE GUI.
Windows PowerShell Get-History Topics
- Introduction to Get-History
- Tasks for Get-History
- Research Properties for Get-History
- Research Parameters for Get-History
- Research the Other Members of the History Family
♣
Introduction to Get-History
All becomes clear when you realize that ‘Get’ is the default verb, thus if you type plain: ‘History’, PowerShell interprets this as ‘Get-History.
Get-History
# History
Note 1: To confirm that History is an alias for Get-History, remove the # in front of History, and run the above commands again.
Note 2: The History cache retains a maximum of 32 commands.
Tasks for Get-History
One reason for listing the history is to save what you have done into a file. Simply pipe (|) the results into a file, thus: History | Out-File "D: \pshell\WorkDone.txt".
Another reason for understanding what’s available in the history cache is to employ the sister cmdlet Invoke-History -Id xyz as a quick method for replaying a long, complex, hard to remember command.
Research Properties for Get-History
To me, the killer reason for using Get-History is to check the execution status; the way I discovered this was with my trusty technique | GM (Get-Member).
Get-History | Get-Member -MemberType Property
Note 3: Another reason for research properties is if you want to modify the results, for example you wish to pipe the output into Format-Table, but are unsure which properties to specify, for example CommandLine and ExecutionStatus.
Research Parameters for Get-History
This is the second of my trilogy of techniques for discovering more about any PowerShell cmdlet.
# PowerShell History Parameters
Clear-Host
Get-Help Get-History -full
Checking the help file will reveal useful parameters, for instance the -count.
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Research the Other Members of the History Family
# PowerShell History Family
Clear-Host
Get-Command -Noun History
This reveals the sister commands: Clear-History and Invoke-History.
Summary of PowerShell Get-History
If you need to check which commands you have been running then ‘History’ is the way to get a list of up to 32 items you recently typed in PowerShell.
If you like this page then please share it with your friends
See more Microsoft PowerShell output tutorials:
• PShell Home • Out-File • Out-GridView • ConvertTo-Csv • ConvertTo-Html • ConvertFrom-Csv
• Tee-Object • Import-CSV • Format-Table • PowerShell Here-String • ConvertFrom-JSON
• Export-CliXml • Format-List • Read-Host • PowerShell Get-History • -f format • Pipe to file
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.