
close
close
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.
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.
advertisment
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.
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.
advertisment
$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 from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group