Doing More with PSReadline History — Part 2
In a previous article, I introduced you to the PSReadline module. This module packs a ton of functionality and I want to continue exploring it today.
Search Command History
As I mentioned in the first article, PSReadline persists command history across PowerShell sessions. These commands do not show when you run Get-History but you can access them with the up arrow or F8. In this screen shot, I have a brand new PowerShell session with only two commands. When I press F8, I can go through the command history.
If you go too far, use Shift+F8 to reverse direction.
You can also search for a particular command from the history. In my command history, I have several commands using Get-Service. At the PowerShell prompt, I can press Ctrl+R and start typing the name of the command. PSReadline will display the most current matching command from the history.
If that is not what I want, I can press Ctrl+R again as often as needed to keep searching back.
If you go too far, you can use Ctrl+S to reverse direction and search forward. When you find the command you want, press Enter to run it or Tab to insert the command at the prompt. Then you can modify or review it.
By the way, you can search for any part of a command, not just the cmdlet.
Here, I started searching for ‘computername’ and PSReadline found a corresponding match. As you can see, the search is not case sensitive.
Modify History Settings
PSReadline has several settings you can configure that control how it manages history. You can use the Get-PSReadlineOption cmdlet to view history related options.
There is no compelling reason to change most of these options. If you do want to change them, you will use Set-PSReadlineOption. Run ‘help set-psreadlineoption -parameter history*’ to learn more about these settings. But for the sake of demonstration, I want to change the number of commands stored in my history and I want to eliminate duplicates.
Set-PSReadlineOption -HistoryNoDuplicates -MaximumHistoryCount 1024
Linux users might also want to enable case-sensitive history searches.
These settings will only apply to the current PowerShell session and only for as long as it is running. If I always want these settings to apply, I can put the command into my PowerShell profile script. Although in my testing, this does not work 100 percent. It appears that even though you can set the maximum history count interactively, it is ignored in a profile script. Instead, PSReadline defaults to the value of the built-in $MaximumHistoryCount variable. That you can change in your profile script. If you assign a new value in your profile script, the PSReadline will also use that value for its MaximumHistoryCount.
If you want to clear your PSReadline history, which is not the same has command line history that you see with Get-History, press Alt+F7 at a PowerShell prompt. There is no confirmation or any indication that anything happened. But if you press the Up arrow key or use Ctrl+R to search, you will get nothing.
Be aware that this does not truly clear PSReadline history. It only clears the history for this session. This does not clear the text file that PSReadline uses to store history. The next time you open a PowerShell session, your history is back. From what I can tell, this is by design. If you want to clear or delete the file itself, try a command like this:
Get-PSReadlineOption | select -expand historysavepath | Remove-Item -whatif
Because you can get the path, you might also want to make a copy.
$pshist = Get-PSReadlineOption | select -expand historysavepath copy $pshist -Destination c:\work\psreadline_history_backup.txt
I hope you will spend some time exploring the history features in PSReadline. Once you get in the habit of using them, you will wonder how you ever managed before. And we are just getting warmed up. I will be back again with more guidance on getting started with PSReadline.
More in PowerShell
How to Check Your PowerShell Version
May 4, 2022 | Michael Reinders
How to Download a File using PowerShell
Apr 21, 2022 | Michael Reinders
How to Write and Run a PowerShell Script File on Windows 11
Apr 4, 2022 | Ivan Mirchev
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Get-AdUser: How to Audit Active Directory Users with PowerShell
Mar 3, 2022 | Michael Reinders
The Top 10 PowerShell Commands That You Should Know
Feb 17, 2022 | Michael Reinders
Most popular on petri