使用PowerShell将输出管道输出到剪贴板

Tah*_*san 22 clipboard powershell

在PowerShell中,如何将命令的输出传递给剪贴板,但是

  • 仍然能够将数据传输到更多进程
  • 不依赖于外部应用程序 clip.exe
  • 因为它可以作为过滤器使用,所以我们立即在命令行上看到输出

编辑:2015年5月14日

3年后,我以为我会分享我ClipboardModule(我希望我被允许):

Add-Type -AssemblyName System.Windows.Forms

Function Get-Clipboard {
    param([switch]$SplitLines)

    $text = [Windows.Forms.Clipboard]::GetText();

    if ($SplitLines) {
        $xs = $text -split [Environment]::NewLine
        if ($xs.Length -gt 1 -and -not($xs[-1])) {
            $xs[0..($xs.Length - 2)]
        } else {
            $xs
        }
    } else {
        $text
    }
}

function Set-Clipboard {
    $in = @($input)

    $out = 
        if ($in.Length -eq 1 -and $in[0] -is [string]) { $in[0] }
        else { $in | Out-String }

    if ($out) {
        [Windows.Forms.Clipboard]::SetText($out);
    } else {
        # input is nothing, therefore clear the clipboard
        [Windows.Forms.Clipboard]::Clear();
    }
}


function GetSet-Clipboard {
    param([switch]$SplitLines, [Parameter(ValueFromPipeLine=$true)]$ObjectSet)

    if ($input) {
        $ObjectSet = $input;
    }

    if ($ObjectSet) {
        $ObjectSet | Set-Clipboard
    } else {
        Get-Clipboard -SplitLines:$SplitLines
    }
}

Set-Alias cb GetSet-Clipboard

Export-ModuleMember -Function *-* -Alias *
Run Code Online (Sandbox Code Playgroud)

我通常使用cb别名(for GetSet-Clipboard),因为它是两种方式,即可以获取或设置剪贴板:

cb                # gets the contents of the clipboard
"john" | cb       # sets the clipboard to "john"
cb -s             # gets the clipboard and splits it into lines
Run Code Online (Sandbox Code Playgroud)

小智 19

如果您有WMF 5.0,PowerShell包含两个新的cmdlet:

获取剪贴板和设置剪贴板


pos*_*ote 1

PSv7 中的本机剪辑 cmdlet

$Host
# Results
<#
Name             : ConsoleHost
Version          : 7.0.3
InstanceId       : 54be9bfd-799d-4213-a13a-22403c1d9ed8
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
#>

Get-Command -Name '*clip*'|Format-Table -a
# Results
<#
CommandType Name                         Version        Source
----------- ----                         -------        ------
Function    Get-Clipboard                1.3.6          PowerShellCookbook
Function    Set-Clipboard                1.3.6          PowerShellCookbook
Function    Start-ClipboardHistoryViewer 0.0            ModuleLibrary
Cmdlet      Get-Clipboard                7.0.0.0        Microsoft.PowerShell.Management
Cmdlet      Set-Clipboard                7.0.0.0        Microsoft.PowerShell.Management
Cmdlet      Set-UDClipboard              2.9.0          UniversalDashboard
Application clip.exe                     10.0.19041.1   C:\WINDOWS\system32\clip.exe
Application ClipRenew.exe                10.0.19041.1   C:\WINDOWS\system32\ClipRenew.exe
Application ClipUp.exe                   10.0.19041.488 C:\WINDOWS\system32\ClipUp.exe
Application rdpclip.exe                  10.0.19041.423 C:\WINDOWS\system32\rdpclip.exe
#>
Run Code Online (Sandbox Code Playgroud)