close

Home

PowerShell

Doing More with PSReadline History — Part 2

Jeff Hicks

|


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.

Accessing command history from PSReadline (Image Credit: Jeff Hicks)

Accessing Command History From PSReadline (Image Credit: Jeff Hicks)


 
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.
Getting the most recent matching command (Image Credit: Jeff Hicks)

Getting the Most Recent Matching Command (Image Credit: Jeff Hicks)


 
If that is not what I want, I can press Ctrl+R again as often as needed to keep searching back.
Continuing to search command history (Image Credit: Jeff Hicks)

Continuing to Search Command History (Image Credit: Jeff Hicks)


 
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.
Searching for other parts of a command (Image Credit: Jeff Hicks)

Searching for Other Parts of a Command (Image Credit: Jeff Hicks)


 
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.

PSReadline history options (Image Credit: Jeff Hicks)

PSReadline History Options (Image Credit: Jeff Hicks)


 
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.

Clear History

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

PowerShell

Understanding PowerShell Objects

Jul 28, 2022 | Jeff Hicks

PowerShell

Using PowerShell Select-Object

Jul 26, 2022 | Jeff Hicks

PowerShell

How to Create Files and Folders with PowerShell

Jun 22, 2022 | Mike Kanakos

PowerShell

How to Use PowerShell For Loop, While Loop, and Other Loops

May 23, 2022 | Ivan Mirchev

PowerShell

How to Check Your PowerShell Version

May 4, 2022 | Michael Reinders

PowerShell

How to Download a File using PowerShell

Apr 21, 2022 | Michael Reinders

Most popular on petri

Article saved!

Access saved content from your profile page. View Saved