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
All becomes clear when you realize that ‘Get’ is the default verb, thus if you type plain: ‘History’, PowerShell interprets this as ‘Get-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.
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.
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.
This is the second of my trilogy of techniques for discovering more about any PowerShell cmdlet.
# PowerShell History Parameters
Get-Help Get-History -full
Checking the help file will reveal useful parameters, for instance the -count.
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.
# PowerShell History Family
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:
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.