Get History of Commands in PowerShell

PowerShell get history of Commands

Using Get-History Cmdlet

Using Get-History to get the complete history of the executed commands in PowerShell.

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:

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

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.

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:

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.

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.

Clear History

Use Clear-History to clear the command history in PowerShell.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *