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 help. How about searching through them or running 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 the 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 the PowerShell command 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 that match the search commands.
Since you get the id along, you can use the 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\PSHistorycommands.xml" Get-History | Export-Csv "Path\PSHistorycommands.csv"
Iin the above sample command, Path represents the complete path of the directory wher you want to save the file ocntaining the PowerShell command history and PSHistorycommands represents the name of the file.
For example, if you want to save the PowerShell history with the name PowerShell Command History at the location “D:\The Windows Club\New folder,” the command will be:
Get-History | Export-Clixml "D:\The Windows Club\New folder\PowerShell Command History.xml"
Get-History | Export-Clixml "D:\The Windows Club\New folder\PowerShell Command History.csv"
Do nt remove the quitations in the above commands. Otherwiise, you will recieve an error.
To import these commands back to another or new session, use the following command.
Add-History -InputObject (Import-Clixml "Path\PSHistorycommands.xml") Add-History -InputObject (Import-Csv "Path\PSHistorycommands.csv")
You can open this CSV or XML file anytime to see the PowerShell command History instantly. This pretty much sums up almost everything around PowerShell Command History, and how you can reuse them using numbers or by searching them or importing them back the next day.
That’s it. I hope this helps.
How do I get PowerShell history to a file?
You can get PowerShell history to a file by exporting it in different formats. PowerShell history can be exported in CSV and XML file formats. You need to execute the required commands to export the PowerShell history to a file.
Does PowerShell save command history?
Yes, PowerShell automatically saves the Command history in a text fle. This file is located at the following location:
C:\Users\username\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine
In the above command, replace the username with your user name. You can open this file in Notepad or any other text editor software.
Read next: How to run PowerShell Scripts without Signing in Windows.