This post will show you how to view the PowerShell command history! The PowerShell command history is a great way to track the commands that have been run on your system. It can be helpful for troubleshooting or for finding a particular command that you need.
Let's get started!
Get-History cmdlet
To view the PowerShell command history, you can use the Get-History cmdlet.
Get-History
Returns:
PS C:\Users\Jeff> Get-History
Id CommandLine
-- -----------
1 Get-History
2 ping google.com
3 clear
PS C:\Users\Jeff>
This cmdlet will return a list of all the commands that have been run in the current session.
Count Parameter
You can also use the -Count parameter to limit the number of returned commands. For example, if you only want to see the last five commands, you would use the following command:
Get-History -Count 5
Returns:
PS C:\Users\Jeff> Get-History -Count 5
Id CommandLine
-- -----------
18 ls ..
19 cd ..
20 ping google.com
21 Get-History
22 Get-History -Count five
PS C:\Users\Jeff>
Specific History Items
To view specific history items, use the Id parameter. For example, to view history item number 1234, type:
Get-History -Id 20
OR
Get-History 20
Returns:
PS C:\Users\Jeff> Get-History -Id 20
Id CommandLine
-- -----------
20 ping google.com
PS C:\Users\Jeff>
Get Detailed History items
This cmdlet is excellent for getting a quick overview of the commands that have been run recently. However, if you want to see more detailed information about each command, you can use the Get-History | Format-List -Property * cmdlet.
Get-History | Format-List -Property *
Returns
PS C:\Users\Jeff> Get-History | Format-List -Property *
Id : 1
CommandLine : Get_History
ExecutionStatus : Completed
StartExecutionTime : 8/20/2022 9:49:40 PM
EndExecutionTime : 8/20/2022 9:49:40 PM
Id : 2
CommandLine : ping google.com
ExecutionStatus : Completed
StartExecutionTime : 8/20/2022 9:49:48 PM
EndExecutionTime : 8/20/2022 9:49:51 PM
PS C:\Users\Jeff>
This cmdlet will return a list of all the commands and additional information about each command. For example, you can see the start and end times when the command was run.
Invoke History
With the Invoke-History cmdlet, you can run a command in the history. This works by providing an ID of the history command you want to invoke.
Example
Get-History
Returns
PS C:\Users\Jeff> Get-History
Id CommandLine
-- -----------
1 Get-History
2 ping google.com
3 clear
PS C:\Users\Jeff>
Once we know the ID of the history command, we Invoke it with the following command.
Invoke-History 2
Returns
PS C:\Users\Jeff> Invoke-History 2
ping google.com
Pinging google.com [172.217.170.206] with 32 bytes of data:
Reply from 172.217.170.206: bytes=32 time=13ms TTL=56
Reply from 172.217.170.206: bytes=32 time=10ms TTL=56
Reply from 172.217.170.206: bytes=32 time=10ms TTL=56
Reply from 172.217.170.206: bytes=32 time=11ms TTL=56
Ping statistics for 172.217.170.206:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 10ms, Maximum = 13ms, Average = 11ms
PS C:\Users\Jeff>
Wrapping up
I hope this article has helped you learn how to view the PowerShell command History. If you have any questions or comments, please leave them below.
Thank you for reading!