[英]Pipe output to the clipboard using PowerShell
See postanote's answer .请参阅postanote 的回答。
After 3 years, I thought I would share my ClipboardModule
(I hope I am allowed to): 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 *
I usually use the cb
alias (for GetSet-Clipboard
) because it is two way ie can get or set the clipboard:我通常使用
cb
别名(对于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
If you have WMF 5.0, PowerShell contains two new cmdlets:如果您有 WMF 5.0,PowerShell 包含两个新的 cmdlet:
get-clipboard and set-clipboard获取剪贴板和设置剪贴板
EDIT: Please look at question instead for solution.
编辑:请查看问题而不是解决方案。
Here is my solution:这是我的解决方案:
Add-Type -AssemblyName 'System.Windows.Forms'
filter Set-Clipboard {
begin {
$cp = @()
}
process {
$_ | Tee-Object -Variable 'cp0'
$cp = $cp + @($cp0);
}
end {
$str = ($cp | Out-String).ToString();
[Windows.Forms.Clipboard]::Clear();
if ( ($str -ne $null) -and ($str -ne '') ) {
[Windows.Forms.Clipboard]::SetText( $str )
}
$cp = @()
}
}
This collects all the objects in an array, $cp
.这将收集数组
$cp
中的所有对象。 We use Tee-Object to redirect the current element, $_
, to both the next process and to store it in the array, $cp
.我们使用Tee-Object将当前元素
$_
重定向到下一个进程并将其存储在数组$cp
中。 Lastly, once the process is finished we set the clipboard's text.最后,该过程完成后,我们设置剪贴板的文本。
I have used it in the following way:我以下列方式使用它:
dir -Recurse | Set-Clipboard | Select 'Name'
And it seems to work.它似乎有效。
To use a function instead:要改用函数:
function Set-Clipboard-Func {
$str = $input | Out-String
[Windows.Forms.Clipboard]::Clear();
if ( ($str -ne $null) -and ($str -ne '') ) {
[Windows.Forms.Clipboard]::SetText( $str )
}
}
Powershell version 6.1 removed this commandlet, so it is no longer built-in. Powershell 版本 6.1删除了此命令行开关,因此它不再是内置的。
Instead, you need to install the ClipboardText package .相反,您需要安装ClipboardText 包。 In Powershell's console type:
在 Powershell 的控制台类型中:
Install-Module -Name ClipboardText
Then you can use:然后你可以使用:
Set-ClipboardText "hello clipboard"
Get-ClipboardText
Here is the github issue with the maintainers of Powershell redirecting you to use the ClipboardText package.这是 Powershell 维护者的 github 问题,重定向您使用 ClipboardText 包。
Native clip cmdlets in PSv7 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
#>
get-clipboard
skips newline characters when text is entered sequentially.按顺序输入文本时跳过换行符。 I use
我用
[System.Windows.Forms.Clipboard]::GetText()
as before.像之前一样。
Now that Get-clipboard and Set-Clipboard are built in PSv7 You can have this function in your profile: "C:\Users<USER_ID>\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1"现在Get-clipboard和Set-Clipboard内置于 PSv7 中,您可以在配置文件中使用此功能:“C:\Users<USER_ID>\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1”
function To-Notepad {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[object]
$InputObject
)
begin { $objs = @() }
process { $objs += $InputObject }
end {
$old = Get-clipboard # store current value
$objs | out-string -width 1000 | Set-Clipboard
& "notepad2" /c
sleep -mil 500
$old | Set-Clipboard # restore the original value
}
}
And then use in this way:然后这样使用:
dir -Path C:\Temp | To-Notepad
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.