Saving a PowerShell command for later use

Let’s suppose the following. You are working with PowerShell console and just finished typing a long and complex command. This second it occurs to you, that you forgot to define a variable, create a remote session or something else. You delete the command, execute some required actions, and type the command again.

Sounds familiar?

In this article we will talk about some techniques, that can help us out.

In order to not distract too much from our main theme, let’s decide, that our long and complex command is the following.

Get-Process -Name $ProcessName

And the missed variable is $ProcessName.

$ProcessName = 'pwsh'

Adding lines

First, we can add the required command just before the one we entered followed by the semicolon.

$ProcessName = 'pwsh'; Get-Process -Name $ProcessName

Or, after entering the Get-Process command we can use Ctrl+Enter shortcut to add an empty sting above the command and type the missed command there.

$ProcessName = 'pwsh'
>> Get-Process -Name $ProcessName

Using clipboard

If you want all your actions to be represented as single commands, you can copy the command to clipboard, execute all missed commands, and paste the command back.

You can do this by selecting the command, for example, using the Ctrl+a shortcut, and copy it by pressing Ctrl+c.

Also, you can copy the command without the need to select it beforehand by using the Ctrl+Shift+c shortcut. It is needed to say, that in PSReadline options this shortcut is referred to as Ctrl+C.

PSReadline

If we open a PSReadline module folder and look through the SamplePSReadLineProfile.ps1 file, among other things, we will find the following.

Set-PSReadLineKeyHandler -Key Alt+w `
                         -BriefDescription SaveInHistory `
                         -LongDescription "Save current line in history but do not execute" `
                         -ScriptBlock {
    param($key, $arg)
​
    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
    [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}

This command binds Alt+w shortcut to the code, specified in the ScriptBlock parameter.

So, when we use the above shortcut, the command typed in the console will be added to the command history and removed from the prompt. And after we execute all the required commands, we can bring the command back by using techniques for working with the command history, for example Up Arrow key.

Concerning the proposed shortcut, you can define it as you see fit. I, personally, would like the key to be closer to the Enter, for example, Ctrl+’. In this case, the code will be as follows.

Set-PSReadLineKeyHandler -Key "Ctrl+'" `
                         -BriefDescription SaveInHistory `
                         -LongDescription "Save current line in history but do not execute" `
                         -ScriptBlock {
    param($key, $arg)
​
    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
    [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}

For you not to enter the aforementioned command in every session, you can add it to the profile.

Return

We have discussed several approaches, that you can use when it occurs, that the command entered requires some other actions to be taken beforehand and we do not feel like entering it again.

Every technique can be of use in different situations, and, as we saw in the last example, we can use the PSReadline module to bind various scripts to keyboard shortcuts, that allows us some freedom to define how our PowerShell console will work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s