Monthly Archive

Categories

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] [<CommonParameters>]

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

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

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

 

For instance:

Set-Clipboard -Value "test"

 

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.

 

The contents of the clipboard are retrieved using Get-Clipboard

£> Get-Clipboard -Raw
test

 

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

£> Get-Command Get-Clipboard -Syntax

Get-Clipboard [-Format <ClipboardFormat>] [-TextFormatType <TextDataFormat>] [-Raw] [<CommonParameters>]

 

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
test

 

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

 

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

Comments are closed.