Managing the PSReadline History File

It has taken some discipline but I have finally gotten the hang of using the command history file maintained by PSReadline. Even though Set-PSReadlineOption has a few options on how to use this file, in my experience I have felt the need to handle a few things on my own. Here’s my take.

I have found that my history file can get quite large which seems to be affecting performance loading PowerShell, or it could just be a coincidence. Even though there are settings to control the maximum number of lines and to keep out duplicate commands, PSReadline doesn’t seem to do anything with these values. Perhaps in earlier versions of Windows it did but I am running 5.1.16299 and nothing seems to keep the history file in check. It is just as easy to handle this myself.

Here is a function I wrote to optimize the PSReadline history file. You can get the code from Github.

The function will trim your history file to a specified number of lines. The default is the global $MaximumHistoryCount variable. The function will also delete duplicate entries, further reducing the file size.  In my PowerShell profile script I dot source the function and run it. Now at least I can keep the file somewhat in check.

If you encounter problems or have feedback, please post an issue on the gist page.

One thought on “Managing the PSReadline History File

Leave a Reply

Your email address will not be published. Required fields are marked *