Powershell - Playing with command history

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

If you use Powershell regularly, it can be interesting to know how to use the history or rather the histories:
- Powershell history (in the current Shell Get-History)
- PSReadLine module history (stored in a file by default)
- The Powershell console cache (command recall with arrows or F8).

Powershell has a command history (default 50).
You can use the up and down arrows to quickly display them.

## Powershell history
# Display a list of commands used in the current session.
Get-History
# Reuse a command from the history
Invoke-History -Id 33
# Export history
Get-History | Export-CSV -Path 'C:\Temp\History.CSV'
# Import history
$History = Import-Csv -Path 'C:\Temp\History.Csv' # Add-History -InputObject
Add-History -InputObject $History
# Clear history
Clear-History
# Delete the last command
Clear-History -count 1 -newest
## History PSReadLine
# Search the history based on the first characters of the F8 command
C<F8>
# F8 as many times as necessary (Shift + F8 to go back)
# You can use F8 directly without specifying any characters
# Search the history using CTRL + R (backwards), CTRL + S (forwards) command matches
CTRL + R
enter the word containing the command you are looking for> Enter
# Launch the last command in the history starting with (thanks to the #)
#get
# Does not work in PowerShell 7
# List the keyboard shortcuts in the history
Get-PSReadlineKeyHandler | Where-Object {$_.function -like '*hist*'}

Complete your reading with the following articles:
Improving Powershell command-line usability with PSReadLine
PowerShell - Command history and confidential information

Related links