Share via


The Clipboard

Talk about a sea change.  I started off wondering how to read text out of the clipboard. Thanks to this link below, that’s trivial, and it even showed me a better way than | clip.exe to stuff it into the clipboard.  My exact words: “Well, that was easy.”

https://stackoverflow.com/questions/1567112/convert-keith-hills-powershell-get-clipboard-and-set-clipboard-to-a-psm1-script

Next, I wondered how to copy a file into the clipboard.  We have the Copy-FileToClipboard function as a result of that.

Finally, I wanted to get the file out of the clipboard, but over RDP.  See, one of the key methods, ContainsFileDropList(), returns file paths on the local machine.  over RDP, that doesn’t translate.  Now, I know the file is in my clipboard because I can Ctrl+V it into File Explorer in the RDP session.  However, ContainsFileDropList always returns $false (and zeros out the clipboard as a side-benefit.)

After two hours of searching, typing, and cursing, my exact words were: “It shouldn’t be this hard.”

So, does anyone know how to paste a file into $home while in an RDP session via PowerShell?  And, no SendKeys() into a File Explorer window does not count.

 

 function Get-ClipBoard {
    <#
    .link
    https://stackoverflow.com/questions/1567112/convert-keith-hills-powershell-get-clipboard-and-set-clipboard-to-a-psm1-script
    #>

    Add-Type -AssemblyName System.Windows.Forms
    $tb = New-Object System.Windows.Forms.TextBox
    $tb.Multiline = $true
    $tb.Paste()
    $tb.Text
}


function Set-ClipBoard() {
    <#
    .link
    https://stackoverflow.com/questions/1567112/convert-keith-hills-powershell-get-clipboard-and-set-clipboard-to-a-psm1-script
    #>

    Param(
      [Parameter(ValueFromPipeline=$true)]
      [string] $text
    )
  
    Add-Type -AssemblyName System.Windows.Forms
    $tb = New-Object System.Windows.Forms.TextBox
    $tb.Multiline = $true
    $tb.Text = $text
    $tb.SelectAll()
    $tb.Copy()
}

function Copy-FileToClipboard {
    param (
      [Parameter(ValueFromPipeline=$true)]
        [string[]]$Path = @()
    ); 

    begin
    {
        $files = New-Object System.Collections.Specialized.StringCollection;
        Add-Type -AssemblyName System.Windows.Forms;
    } # begin

    process
    {
        $Path | 
        % { 
            if (Test-Path $_) 
            { 
                $files.Add((Resolve-Path -Path $_).ProviderPath); 
            } # if (Test-Path $_)
        } |
        Out-Null; 
    } # process

    end
    {
        [System.Windows.Forms.Clipboard]::SetFileDropList($files);
    } # end
}