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)

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)

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)

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)

Here, I started searching for ‘computername’ and PSReadline found a corresponding match. As you can see, the search is not case sensitive.

Subscribe to Petri Newsletters Office 365 Insider Our Petri Office 365 Insider is dedicated to sharing detailed knowledge from top Office 365 experts. Delivered once a month to your inbox. All Newsletters Petri.com may use your contact information to provide updates, offers and resources that may be of interest to you. You can unsubscribe at any time. To learn more about how we manage your data, you can read our Privacy Policy and Terms of Service. !Already a Petri.com member? Login here for 1-click registration.

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)

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.



1 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:



1 Get-PSReadlineOption | select -expand historysavepath | Remove-Item -whatif



Because you can get the path, you might also want to make a copy.



1 2 $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.