23

編集:2020年10月23日

postanote の回答を参照してください。

編集: 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 *

私は通常、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
4

5 に答える 5

21

WMF 5.0 を使用している場合、PowerShell には次の 2 つの新しいコマンドレットが含まれています。

get-clipboard と set-clipboard

于 2016-02-05T17:39:02.780 に答える
3

編集:解決策については、代わりに質問をご覧ください。

これが私の解決策です:

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 = @()
    }
}

これにより、配列内のすべてのオブジェクトが収集されます$cpTee-Objectを使用して、現在の要素 を次のプロセスにリダイレクト$_し、配列 に格納し$cpます。最後に、プロセスが終了したら、クリップボードのテキストを設定します。

私は次の方法でそれを使用しました:

dir -Recurse | Set-Clipboard | Select 'Name'

そしてそれはうまくいくようです。

代わりに関数を使用するには:

function Set-Clipboard-Func {
    $str = $input | Out-String

    [Windows.Forms.Clipboard]::Clear();

    if ( ($str -ne $null) -and ($str -ne '') ) {
        [Windows.Forms.Clipboard]::SetText( $str )
    }
}
于 2012-10-29T18:30:46.360 に答える