WMF 5.0 – Clipboard cmdlets

The April 2015 WMF 5.0 preview brings new functionality in the shape of cmdlets for working directly with the clipboard.

You use Set-Clipboard to put data onto the clipboard

£> get-command Set-Clipboard -Syntax

Set-Clipboard [-Append] [-WhatIf] [-Confirm] []

Set-Clipboard [-Value] <string[]> [-Append] [-WhatIf] [-Confirm] []

Set-Clipboard -Path <string[]> [-Append] [-WhatIf] [-Confirm] []

Set-Clipboard -LiteralPath <string[]> [-Append] [-WhatIf] [-Confirm] []

You can add text or files to the clipboard. Notice that you can use –Append to add to the clipboard content rather than overwriting any existing data.

For instance:
Set-Clipboard -Value “test”

The contents of the clipboard are retrieved using Get-Clipboard

£> Get-Clipboard -Raw

As well as raw data you can pull data in a number of formats.

£> Get-Command Get-Clipboard -Syntax

Get-Clipboard [-Format ] [-TextFormatType ] [-Raw] []

Format can be one of: Text, FileDropList, Image, Audio

TextFormat type can be one of:  Text, UnicodeText, Rtf, Html, CommaSeparatedValue

These all produce the same result:

£> Get-Clipboard -Raw

£> Get-Clipboard -TextFormatType Text
£> Get-Clipboard -Raw
£> Get-Clipboard -Format Text
£> Get-Clipboard -Raw

Notice how use the TextFormatType Text or Format Text cause get-Clipboard to be called again with the –Raw parameter

One obvious and useful tasking for the clipboard cmdlets is copying commands between PowerShell sessions:

On the source machine

£> Get-History -Id 43

Id CommandLine
— ———–
43 Find-Package -Name PSReadline -Source PSGallery | fl *

Set-Clipboard -Value (Get-History -Id 43 | select -ExpandProperty Commandline)

On the target machine:

Invoke-Expression -Command (Get-Clipboard -Raw)

This is one set of cmdlets that will generate many more uses as you experiment with them

This entry was posted in PowerShell v5. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s