Introduction

The post is a collection of tips and tricks I’ve gathered that make working at the command line easier. Some require a little effort to retrain some habits. For example using a control key sequence instead of hitting the up arrow 20 times or gasp reaching for the mouse. My goal for this post is to reduce the keystrokes, and therefor time, it takes to get a command written.

PSReadLine

PSReadLine is a module that come with PowerShell. When you type a command and it changes color, that’s PSReadLine at work. Also tab completion of commands and parameters. For the longest time, that’s all I used it for. When version 2.1.0 came out, they added the option to try and guess what you are trying to type based on what’s in your PSReadLine history file. This shows up as a dimmer color for the rest of the line when you start typing. The tab key will allow you to choose it. With version 2.2.2, the option was added to have the prediction shown in a dropdown list. As of this writing, 2.2.6 is the current version.

PSReadLineKeyHandler

With the latest version, some of these settings are the default.

    
        
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
    
These two are now defaults. At first glance they don’t seem that impressive. Sure the up arrow scrolls through your history. The impressive and incredibly useful part comes when you type the being of a command and then use the up arrow. This now scrolls through your history showing only the thing that start with what you typed.

ctrl + r will open a search dialog to search through your history.

    
        
Set-PSReadLineKeyHandler -Key Escape -Function RevertLine;
    
This sets the behavior of the esc key. On Windows the default it to clear the command line. On Mac and Linux there are a lot of sequences that start with escape and another letter. Setting the escape key to Windows behavior will break all those sequences.

Tab completion. If you type the beginning name of a cmdlet and hit tab and PSReadLine will try to finish the command. Hit tab again and it will try the next one. Type a dash to start a parameter, tab will complete that. If the module isn’t loaded yet, PSReadLine will import it so it can discover the available parameters.

shift + tab will cycle backwards through the options.

ctrl + space will list all options at once.

ctrl + l will save what you typed so far, clear the screen and put what you type back on the command line.

Running this will show you all the key bindings currently in place.

    
        
Get-PSReadLineKeyHandler
    
Take the time to look through these and start trying to add them to your daily routine. There’s some gold buried in there that I haven’t seen documented anywhere.

PSReadLineOption

    
        
Set-PSReadLineOption -HistoryNoDuplicates:$true;
    
Setting the history to no duplicates is a bit misleading. The duplicates are still saved to your history file (and count toward maximum history count) but are not displayed when scrolling through your command history. If you ran the same command 20 times looking for an update, you likely don’t want to scroll through all 20 to get to a command further back.

    
        
Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true;
    
By default the cursor stays where you were when you scroll back. This is a matter of personal preference. I like the cursor at the end.
    
        
Set-PSReadLineOption -MaximumHistoryCount
    
The default is 4096. This isn’t lines but commands. So a multiline command only counts as one in the history file. I haven’t tried setting this number higher. It’s defined as an Int32 so you can go much higher. My only concern would be performance. PSReadLine still has to manage that history in memory.

    
        
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle InlineView
    
The prediction source defaults to none. If you set it to history, PSReadLine will start giving you intellisense style predictions from your history.

Prediction view style will either show inline (default) or in a drop down list. F2 will toggle between the two.

    
        
Get-PSReadLineOption
    
This will show you what is currently set as well as show what your options are.

New-PSDrive

    
        
$null = New-PSDrive -Name S01 -PSProvider FileSystem -Root \\USHQSERVER01\share -Description "C Drive on SERVER01" 
$null = New-PSDrive -Name S37 -PSProvider FileSystem -Root \\USHQSERVER37\share -Description "C Drive on SERVER37" 
function S01: { Set-Location $MyInvocation.MyCommand.Name }
function S37: { Set-Location $MyInvocation.MyCommand.Name }
    
PowerShell allows you to get creative with drive names. You’re not limited to just drive letters. On non-Windows systems this isn’t really useful. The functions all for just entering the drive name to switch to it. So you can use Set-Location S01: or just S01:

$PSDefaultParameterValues

    
        
$PSDefaultParameterValues['Get-DbaRegisteredServer:SqlInstance'] = 'Server01'; 
$PSDefaultParameterValues['Get-DbaRegisteredServer:Group'] = 'AllServers';
$PSDefaultParameterValues['Enter-PSSession:ConfigurationName'] = 'PowerShell.7';
$PSDefaultParameterValues['Enter-PSSession:ConfigurationName'] = 'PowerShell.7';
    
Adding defaults to commands you run frequently is very handy.

Variables

    
        
$s01 = 'Server01';
$s42 = 'Server42.ourcompany.com';
    
Setting variables for common items is a timesaver also.

Functions

    
        
function clerr { $Error.Clear() }
    

    
        
function standardmsg {
    param(
        $name = 'Valued Customer'
        )
    $msg = @"
    Thank you $name,
    We are looking into your concern. Someone from the support team will contact you shortly.
    "@;
    $msg | Set-ClipBoard;
}
    
Simple functions can really add to your workflow. They also help you to start building your toolbox to use elsewhere.

Conclusion

There are lots of hidden gems in the tools that come with PowerShell. Some of them are just things “everyone knows” but newcomers don’t. If do a search for PowerShell profile examples, you can find where other bloggers have posted their profile and you can look for all the time saving goodness that they have to offer.

Be sure to check out the whole series on getting the most out of the PowerShell prompt.

Thanks for reading.
-Jeff