Table of Contents
Using Get-History
Cmdlet
Using Get-History
to get the complete history of the executed commands in PowerShell.
1 2 3 |
Get-History |
1 2 3 4 5 6 7 8 9 10 11 |
Id CommandLine -- ----------- 1 Get-History 2 ls 3 ping example.com 4 Get-History 5 Get-History 6 Get-Process 7 Get-Service |
In this example, the history of the commands is obtained using the Get-History
cmdlet. On execution of the above code, we can see the list of executed commands is returned and displayed on the screen with their respective Id
(showing the sequence of the execution of the commands) and the CommandLine
(showing the text or command we entered). From the above output, we can see 7
commands were executed in PowerShell.
By default, Get-History
does not display all the object properties on execution. To display all the object properties on the console consider the below example:
1 2 3 |
Get-History | Select-Object -Property * |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Id : 1 CommandLine : Get-History ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:37:00 PM EndExecutionTime : 7/19/2023 3:37:00 PM Id : 2 CommandLine : ls ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:37:04 PM EndExecutionTime : 7/19/2023 3:37:05 PM Id : 3 CommandLine : ping example.com ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:37:58 PM EndExecutionTime : 7/19/2023 3:38:02 PM Id : 4 CommandLine : Get-History ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:38:13 PM EndExecutionTime : 7/19/2023 3:38:13 PM Id : 5 CommandLine : Get-History ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:52:33 PM EndExecutionTime : 7/19/2023 3:52:33 PM Id : 6 CommandLine : Get-Process ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:53:13 PM EndExecutionTime : 7/19/2023 3:53:14 PM Id : 7 CommandLine : Get-Service ExecutionStatus : Completed StartExecutionTime : 7/19/2023 3:53:25 PM StartExecutionTime : 7/19/2023 3:53:25 PM |
In this code, the output of Get-History
is passed to the Select-Object
cmdlet using the pipe operator (|
) with the -property *
option to get all the object properties on the console. We can observe, the given command returned Id
, CommandLine
, ExecutionStatus
and the duration of the command.
Get Specific Commands from the History
If you want to get the last three executed commands from the history check the below example
1 2 3 |
Get-History -Count 3 |
1 2 3 4 5 6 7 |
Id CommandLine -- ----------- 5 Get-History 6 Get-Process 7 Get-Service |
In the above example, the -Count
option is used with the Get-History
cmdlet to get the last 3
executed commands from the history. Consider the below example if you want to get the range of commands.
Note: You can change the value of -Count
according to your requirement. For example, if you want to get the list of the last 20 executed commands run set count as -Count 20
.
Re-execute Commands from History
If you want to run a specific command again from the history that you have executed. Consider the below example.
1 2 3 |
Invoke-History -Id 6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Get-Process Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 336 19 9204 24588 0.14 11012 5 ApplicationFrameHost 624 44 29696 1492 1.11 16968 5 CalculatorApp 285 19 21348 43392 0.50 2152 5 chrome 387 24 227000 202836 65.38 2772 5 chrome 445 23 92284 130664 10.91 3184 5 chrome 283 19 21556 38792 0.28 3564 5 chrome 325 22 64852 108200 15.77 6476 5 chrome 370 24 110008 134700 7.47 7424 5 chrome 246 16 15172 19488 0.56 7628 5 chrome 1001 44 152880 139736 172.61 7644 5 chrome 293 20 101448 71412 10.50 7800 5 chrome 292 20 39724 62776 1.64 8096 5 chrome 438 23 139896 165516 48.23 8832 5 chrome 279 19 21992 41580 0.38 9356 5 chrome |
In this code snippet, the Invoke-History
cmdlet is used to invoke the History command and execute the command with the given id. In the above case, the command id 6
is executed which is the Get-Process
cmdlet. Another way to write the same command is as follows:
1 2 3 |
!6 |
In this way, you can re-execute the command from the history using the exclamation mark (!
) followed by the command id
. On execution of this command, you will get the same output as above.
Save the Command History to a File
If you want to save the history of executed commands for the current session in the form of a CSV file for future use check the following example.
1 2 3 |
Get-History | Export-CSV -Path 'C:\History\CommandHistory.Csv' |
In this PowerShell script, the output of Get-History
is passed to the Export-CSV
cmdlet as input using the |
pipe operator. Here, the -Path
option is used to specify the path where we want to save the history commands in the form of CSV.
On execution of the above command, no output is displayed on the screen. However, the list of executed commands is stored at path C:\History\CommandHistory.Csv with all the object properties.
Note: The history of executed commands in the current session is stored in memory by default. Once the PowerShell window is closed the history will be lost. To store the history between sessions by default configure the PowerShell to save the history to a file.
Further reading:
Clear History
Use Clear-History
to clear the command history in PowerShell.
1 2 3 |
Clear-History |
By executing the above command the history of the current PowerShell session is removed.
That’s all about how to get history of Commands in PowerShell.