Windows PowerShell has a built-in History feature that remembers all the commands you executed when using it. While it should remember the History of the active session, I see that it retains more than that. In this post, I will show how you can see PowerShell command History and use it smartly.
How to see PowerShell Command History
The most significant advantage of having a History feature is you don’t have to type again to execute a particular command you already executed once. However, recursing through hundreds of commands is not much of help. How about searching through them or run a specific command you only remember partially? Follow these methods.
- Keyboard Method
- Search Forward or Backward in PowerShell History
- View the entire History of this session
- Invoke a particular command from PowerShell command History
- Search within History
In the end, we have also explained how you can clear, export, and import PowerShell command’s History. Very handy if you need to execute the same commands often.
1] Keyboard Method
I am sure you have used the Command Prompt once if you have used Windows for some time. If you use the arrow keys up and down, you can go back and forth of the executed commands. The same applies to PowerShell as well. But here is the twist. If you remember a command partially, here is how to search.
- Type part of the command you remember
- Press F8, and keep pressing it until you find.
- The search term will be highlighted with a fluorescent green, followed by the rest of the command.
The only drawback is that the search term has to be the start of the command.
2] Search Forward or Backward in History
If you want to search forward or backward in History, then you can use Ctrl + R and Ctrl + S. The former lets you search back in History while the later forward. As soon as you use the keyboard shortcut, you should get a snippet at the bottom (bck-i-search or fwd-i-search). Type what you want to search, and even if the text belongs to somewhere in the middle of the command, it will search for it. It is much better compared to the F8 method we used above.
3] View the entire PowerShell command History of this session
Type History, and press the Enter key. You should be able to see all the commands you executed in the current session. While you can view the commands executed in the earlier session using the arrow keys, the History will not have it. If you press the key “H” and hit enter, it will do reveal the History of commands. Consider it as a shortcut to the History command.
4] Invoke a particular command from History
If you notice, the image above has an ID next to each command. If you want to execute one specific command from the list, you need to use Invoke-History.
- Note the ID of the command you want to execute
- Type Invoke-History <ID>. Replace <ID> with the exact number
- Press the Enter Key, and it will execute that command from History.
5] Search within History
The easiest way to search within History of commands is to use the Select-String method on History Output.
- Type the following, and replace Get by the term you want to search
- Get-History | Select-String -Pattern “Get.”
- It will reveal all the commands which match the search commands.
Since you get the id along, you can use Invoke History command to execute it instantly.
Clear, Export, and Import PowerShell History
To delete all the commands from History, all you need to do is execute the command “Clear-History.” Do note that even after doing this, you can still access the command using up and down arrow keys.
To export all the commands, you can use the Export-Clixml or Export-CSV format. Here is the sample command
Get-History | Export-Clixml -Path <Path>\PSHistorycommands.xml
Get-History | Export-Csv -Path <Path>\PSHistorycommands.csv
To import these commands back to another or new session, use the following command.
Add-History -InputObject (Import-Clixml -Path <Path>\PSHistorycommands.xml)
Add-History -InputObject (Import-Csv -Path <Path>\PSHistorycommands.csv)
You can open this CSV or XML file anytime to see PowerShell command History instantly. This pretty much sums up almost everything around PowerShell Command History, and how you can reuse them using cumbers or by searching them or importing them back the next day.