Tuesday, 25 September 2018

Search through previous powershell commands with grep

 

 

It can be really difficult trying to remember that command you used a few weeks ago in Powershell.  Here’s an easy way to search through your previous commands to find the right one and jog your memory. First, run

 

 

(Get-PSReadlineOption).HistorySavePath

C:\Users\username\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

 

 

Then you can cat the output to look for the command

 

 

cat C:\Users\username\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

 

 

Or just open it in notepad

 

 

notepad C:\Users\username\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

 

 

If you want to search for a specific command within your history you can use findstr as follows

 

 

cat C:\Users\username\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt | findstr -i get-azureadgr

 

 

Or if you’re more comfortable with the UNIX/Linux commands like myself you can use grep in Powershell by creating an alias as follows

 

 

new-alias grep findstr

 

 

And search for the command as before, but this time using grep 😊

 

 

cat C:\Users\username\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt | grep -i get-azureadgr

 

No comments:

Post a comment