Let PSReadLine Handle PowerShell — Part 4

In our exploration of the magic you can find in PSReadline, we have kept it pretty simple. But there is one more part of PSReadline that personally, I am finding extremely useful. The more I try it out, the more I realize how much value it adds to my work process. Although, I will warn you that to really leverage this feature will require some scripting experience. You could be dealing with parts of the .NET Framework. Or you can simply cut and paste my examples!

PSReadline can take advantage of something called a key handler. You have already seen some of them in action like the Up and Down arrow keys. In short, PSReadline executes a chunk of code when certain keys or key combinations are detected. Many of these are set for you automatically when you import the PSReadline module. You can also define your own custom settings.

History Handler

The first type of handler is actually something a bit specialized. You already know from a previous article, that PSReadline maintains a text list of your command history. But perhaps you would like some control over that process. With PSReadline, you define an AddToHistoryHiandler. This is a PowerShell scriptblock that returns True or False. If True, then the command is added to your PSReadline history. Here is a scriptblock that returns True if the length of the command is 5 or more characters. This eliminates short commands like 1+1.

$sb = {
#scriptblock returns true or false to indicate if command should be saved in history
    if ($line.length -ge 5) {
        return $True
    else {
        return $False
    #this could also be written as
    # return $line.length -ge 5

Once defined, I can add it to my session:

Set-PSReadlineOption -AddToHistoryHandler $sb

Longer commands are stored in the history file but shorter ones are not.

Testing a PSReadline History Handler (Image Credit: Jeff Hicks)
Testing a PSReadline History Handler (Image Credit: Jeff Hicks)

There is no limit to what you can test for or as complicated as you need to be. You only need to ensure that your scriptblock returns True or False. Here is a handler that only saves a command to history if it is longer than 5 characters and is not a help command.

Set-PSReadlineOption -AddToHistoryHandler {
return ($line.length -ge 5 -AND $line -notmatch "^get-help|^help")

As with other PSReadline settings, this will only last for the duration of your PowerShell session. If you want to always have this setting, add the commands to your PowerShell profile script. If you need to stop using the handler in your session, you can set it back to $null.

Set-PSReadlineOption -AddToHistoryHandler $null

Other Key Handlers

The other types of handlers are even more impressive. Many of the handlers are built on members of the [Microsoft.PowerShell.PSConsoleReadLine] .NET class.

PSConsoleReadline Class Members (Image Credit: Jeff Hicks)
PSConsoleReadline Class Members (Image Credit: Jeff Hicks)

I will not go through every item, many of them are already in use. You can check your current key handlers with Get-PSReadlineKeyHandler.
Get PSReadline Key Handlers (Image Credit: Jeff Hicks)
Get PSReadline Key Handlers (Image Credit: Jeff Hicks)

You will recognize some of the keyboard shortcuts I demonstrated in previous articles. But let’s add some other PSReadline tricks.

Set-PSReadlineKeyHandler -key F12 -Function CaptureScreen

The -Key parameter is an alias for -Chord. The parameter indicates what key combination or key to use. In this case, the F12 key. The value for -Function is one of the class methods. In this case, something that will capture the console screen. You can see all of the possible values in the help documentation.

For this particular function, when I press F12, PSReadline selects the current line. I can then use the Up and Down arrows to select additional parts of the console screen.

Capturing the console with PSReadline (Image Credit: Jeff Hicks)
Capturing the Console with PSReadline (Image Credit: Jeff Hicks)

When I hit Enter, the text will be copied to the clipboard and I can paste it in another application.
Pasted console output (Image Credit: Jeff Hicks)
Capturing the Console with PSReadline (Image Credit: Jeff Hicks)

As with everything else PSReadLine-related, if you want this keyhandler all the time, add the command to your PowerShell profile script.

Are you intrigued? I am just getting started. Next time, we will look at some advanced handlers.