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:
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:
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:
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:
Specific History Items
Use the -Id parameter to view specific history items. To access history item number, for example, type 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:
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.