PowerShell keeps the history of executed commands.
PowerShell itself keeps them in memory. In addition to this, the PSReadline module saves them in a file on the hard drive. that allows us to go back to command entered not only in the current session, but also executed some time ago and probably in different PowerShell version.
We can get the location of this file as follows.
Get-PSReadLineOption | Select-Object HistorySavePath
To look at the previously entered commands we can use the Up Arrow and Down Arrow keys. The former lets us go back in history, and the latter sends us in opposite direction. By using these keys, we can traverse the full history – both from memory and the file.
History cmdlets
Unlike the Up Arrow and Down Arrow keys, the history cmdlet, like Get-History, Invoke-History, and Clear-History, interact only with the commands, stored in memory.
So, the Get-History cmdlet, or its aliases – h, history, and ghy – return only those commands, that was executed in the current session. For example.
Get-History
Id Duration CommandLine -- -------- ----------- 1 0.143 Get-Process pwsh 2 0.018 Get-Service WinRM 3 0.023 Get-Content C:\Windows\System32\drivers\etc\hosts
The Invoke-History cmdlet (its aliases – r and ihy) allows us to execute some command from the history.
Invoke-History 2
Status Name DisplayName ------ ---- ----------- Running WinRM Windows Remote Management (WS-Management)
And the Clear-History cmdlet (its alias – clhy) – clears the history. Of course, this affects only the history, stored in memory, and do not affect the content of the file in any way.
# symbol
To execute a command from memory we can enter the # symbol, followed by the command id and press Tab or Ctrl+Space.
#2[Tab] => Get-Service WinRM
Also, we can use the # symbol for searching a command in the history. For example, like this.
#hosts[Tab] => Get-Content C:\Windows\System32\drivers\etc\hosts
If the fragment we specify occurs in several command, we can look through them by using Tab, or, to move in opposite direction – Shift+Tab. Also, to get all found commands at once we can use Ctrl+Space.
PSReadline
Concerning a search through the full history, we have several options provided by the PSReadline module.
To search for the commands, starting with the specified characters, we can use F8, and, again, to move in opposite direction – Shift+F8.
Get[F8] => Get-Content C:\Windows\System32\drivers\etc\hosts
To search by arbitrary part of the command we can use the Ctrl+R and Ctrl+S hotkeys. The former is used to go back in history, and the latter – to move forward.
[Ctrl+R]WinRM
By typing the above symbols, we can find the Get-Service WinRM command. To search deeper in the history, we can continue pressing Ctrl+R, or, to return to previous findings – Ctrl+S.
Empowering the environment
If we look at the SamplePSReadLineProfile.ps1 file located in the PSReadline module folder, we can find in there some suggestions, concerning the use of different features of the module.
One of them is assigning the Up Arrow and Down Arrow keys the functions of the F8 and Shift+F8, respectively.
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
If we add the above commands to the PowerShell profile, we will be able to use the Up Arrow and Down Arrow keys not only for traversing the history, like is was before, but also to search for the commands, starting with the specified symbols.
Return
We discussed only a small part of what PowerShell and the PSReadline module offer to create a more comfort environment for working in the console, but the ability to reuse commands and traverse command history quickly and efficiently is one of the most importance.
2 thoughts on “PowerShell command history”