Let PSReadLine Handle PowerShell — Part 4

Posted on September 20, 2017 by Jeff Hicks in PowerShell with

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.

Once defined, I can add it to my session:

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.

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.

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.

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.

Sponsored

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

Tagged with ,

Register for this Webinar

How Replication Supports Your Company’s RTOs & RPOs
Join us for this free webinar

Can you have your workloads running within the agreed RTOs? Join this webinar with expert speakers from Veeam to exceed business objectives with an RPTO<15 min for ALL of your application and data.

Thursday, December 14, 2017 at 11 a.m EST