Powershell: Set-Clipboard in PowerShell Core on Windows Results in Throwing CommandNotFoundException

Created on 20 Jul 2017  路  14Comments  路  Source: PowerShell/PowerShell

Set-Clipboard throws a CommandNotFoundException in PowerShell Core on Windows. There is a similar issue #3618 open for Mac. A workaround could be piping to clip.exe in Windows but this passes functionality out of PowerShell.

Steps to reproduce

Get-Content -Path myfile.txt | Set-Clipboard

Expected behavior

Text piped or passed into command should get set to the clipboard.

Actual behavior

Get-Content -Path myfile.txt | Set-Clipboard
Set-Clipboard : The term 'Set-Clipboard' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:32
+ Get-Content -Path myfile.txt | Set-Clipboard
+                                ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Set-Clipboard:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Environment data

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.4
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


Area-Cmdlets-Management Resolution-Fixed

Most helpful comment

As a stopgap, here are functions Set-ClipboardText and Get-ClipboardText that work with both editions (Windows PowerShell and PowerShell Core) on all supported platforms.

In PowerShell Core, they use clip.exe, pbcopy / pbpaste, xclip on Windows / macOS / Linux, and on Windows PowerShell they use WinForms on v5.0- and defer to Set-Clipboard / Get-Clipboard on v5.1+.

The caveat is that, on Linux platforms, xclip is neither preinstalled nor guaranteed to be available (different Linux desktop environments have different utilities).

All 14 comments

Hum! Set-Clipboard doesn't exist in PowerShell Core (yet).
:)

@SteveL-MSFT Have we plans to port Get/Set-Clipboard cmdlets? On Windows? On Unix?

@iSazonov Clipboard class is part of WinForms so no hope of having it in corefx. Personally, I think the cmdlets are useful, but we'll have to use native apis to make it work.

PSReadline needs this functionality as well, but I would probably just copy the code so the dependency on WinForms can be removed completely (not use WinForms with full clr.)

As a stopgap, here are functions Set-ClipboardText and Get-ClipboardText that work with both editions (Windows PowerShell and PowerShell Core) on all supported platforms.

In PowerShell Core, they use clip.exe, pbcopy / pbpaste, xclip on Windows / macOS / Linux, and on Windows PowerShell they use WinForms on v5.0- and defer to Set-Clipboard / Get-Clipboard on v5.1+.

The caveat is that, on Linux platforms, xclip is neither preinstalled nor guaranteed to be available (different Linux desktop environments have different utilities).

@mklement0 perhaps you can wrap it as a module and publish to PowerShell Gallery?

@SteveL-MSFT: Your wish is my (belated) command: I've published module ClipboardText:

I think @mklement0's module is a fine solution to this issue.

How can we make better discoverability for this module?

@iSazonov well, I tweeted it so that helps. Perhaps someone can add it to https://github.com/janikvonrotz/awesome-powershell?

The cmdlets is documented for Windows PowerShell. Maybe for PowerShell Core we could replace the articles with link on @mklement0 module?

@iSazonov if we ever get around to implementing https://github.com/PowerShell/PowerShell/issues/1982, I think that should solve the discovery problem

Although .Net Core 3.0 brings back WinForms so the Windows implementation of the Clipboard cmdlets may come back, it wouldn't be cross platform compatible. My suggestion is to use code similar to what we have in PSReadLine for cross platform clipboard support: https://github.com/lzybkr/PSReadLine/blob/194c8dba426986f115c61919e8699b0f34fc4b01/PSReadLine/Clipboard.cs#L12

:tada:This issue was addressed in #10340, which has now been successfully released as v7.0.0-preview.6.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andschwa picture andschwa  路  3Comments

manofspirit picture manofspirit  路  3Comments

garegin16 picture garegin16  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments

pcgeek86 picture pcgeek86  路  3Comments