PowerShell

How to see PowerShell Command History on Windows 10

How to see PowerShell Command History on Windows 10

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.

  1. Keyboard Method
  2. Search Forward or Backward in PowerShell History
  3. View the entire History of this session
  4. Invoke a particular command from PowerShell command History
  5. 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.

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.

5] Search within History

The easiest way to search within History of commands is to use the Select-String method on History Output.

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 \PSHistorycommands.xml Get-History | Export-Csv -Path \PSHistorycommands.csv 

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 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.

Gry Darmowe i otwarte silniki gier do tworzenia gier na Linuksa
Darmowe i otwarte silniki gier do tworzenia gier na Linuksa
Ten artykuł zawiera listę darmowych i otwartych silników gier, których można używać do tworzenia gier 2D i 3D w systemie Linux. Istnieje wiele takich ...
Gry Samouczek Shadow of the Tomb Raider dla systemu Linux
Samouczek Shadow of the Tomb Raider dla systemu Linux
Shadow of the Tomb Raider to dwunasty dodatek do serii Tomb Raider - przygodowej serii gier akcji stworzonej przez Eidos Montreal. Gra została dość do...
Gry Jak zwiększyć FPS w Linuksie??
Jak zwiększyć FPS w Linuksie??
FPS oznacza Klatki na sekundę. Zadaniem FPS jest pomiar liczby klatek na sekundę podczas odtwarzania wideo lub wydajności gier. W prostych słowach lic...