Powershell

How Do I Search PowerShell Command History

PowerShell is a cross-platform task automation solution that allows you to run commands and scripts to perform various tasks. PowerShell also keeps a record of the commands you have entered in the current and previous sessions, which is known as the command history. You can use the command history to review your work, recall and run previous commands, or search for specific commands. In this article, we will show you how to search PowerShell command history using different methods.

How Do I Search PowerShell Command History

For searching through your PowerShell command history, use the Get-History cmdlet. It retrieves the session history and gives the list of commands executed during the ongoing session. You can use this cmdlet to view and filter your command history using various parameters and operators. To see the history of commands you’ve typed, use the following cmdlet:

Get-History

Find Detailed History Items

This cmdlet proves handy in swiftly summarizing recently executed commands. Run the following cmdlet to see a more thorough command history that includes the execution status of each command as well as its start and end times:

Get-History | Format-List -Property *

To search your history, pipe the output to the Select-String cmdlet and provide the text to be searched for and replace “command” with the text you wish to search for in the cmdlet below:

Get-History | Select-String -Pattern "<command>"

Count Parameter

Use the -Count argument to tell PowerShell how many history entries to display in order to read or search through more items in the past. For example, to show 10 entries, run the following cmdlet:

Get-History -Count 10

Specific History Items

Use the -Id parameter to view specific history items. To access history item number, for example, type 14:

Get-History -Id 14

Invoke History

You can perform a command in the history using the Invoke-History cmdlet. This works by giving the ID of the history command you want to run:

Invoke-History 5

Conclusion

PowerShell command history is a useful feature that helps you keep track of your work and reuse previous commands. You can search PowerShell command history using Get-History cmdlet and there are certain parameters that can be used with this cmdlet to have a more detailed search of the previously executed commands.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.