Copy Filenames to Clipboard with PowerShell

Have you ever wanted to copy a large amount of data to the Windows clipboard, but haven’t known quite how to do it? How about this scenario: you have a folder full of files, and you want to get a list of the files’ full names / paths. That’s pretty easy to do with a simple Get-ChildItem PowerShell command, but how do you then get the data into say, an e-mail, or paste it into a file? Sometimes it’s just easiest to have the data copied to the Windows clipboard for a more arbitrary usage.

This simple PowerShell script will take a folder path and copy all of the file paths to the Windows clipboard.

Important: The Windows clipboard requires that the application be running in Single-Threaded Apartment (STA) mode, so if you’re using PowerShell v2, please be sure to run PowerShell with the –STA parameter. If you use Multi-Threaded Apartment (MTA), the [Clipboard]::SetText() static method will fail.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function Copy-FileNamesToClipboard {
    # The CmdletBinding() attribute
    [CmdletBinding()]

    # Define parameters for the PowerShell v2 advanced function
    param (
        # Define a mandatory parameter called Path, which will be the path to gather files from
        [Parameter(Mandatory = $true)]
        [string] $Path
    )

    process {
        # Load the Presentation Core assembly, which contains the System.Windows.Clipboard class
        [void][Reflection.Assembly]::LoadWithPartialName("PresentationCore");

        # Initialize the text variable to an empty string
        $text = [string]::Empty;

        # Get a list of files in the specified directory
        $FileList = Get-ChildItem -Path $Path;

        # For each file in the directory, add the file path and a new line to $text
        foreach ($File in $FileList) {
            $text += ("{0}`n" -f $File.FullName);
        };

        # Set the Clipboard text to the string we just got
        [System.Windows.Clipboard]::SetText($text);
    };
};

Copy-FileNamesToClipboard -Path c:\Users\Trevor\Documents\;
  • http://twitter.com/pcgeek86/status/168046568182784000/ Trevor Sullivan (@pcgeek86)

    Copy Filenames to Clipboard with #PowerShell | Trevor Sullivan’s Tech Room – http://t.co/OhjsyzYU

  • codecontemplator

    Indeed, it is very useful to send data to the clipboard at times. If you have powershell community extensions available you can use the Out-Clipboard (aliased ocb):

    gci | select fullname | ocb 

  • http://twitter.com/dfinke/status/168125712975396864/ doug finke (@dfinke)

    Copy Filenames to Clipboard with #PowerShell http://t.co/39OUNSAy authored by @pcgeek86

  • http://www.leporelo.eu/ Pepa

    What will you do when you need to copy directory names? Or only *.log files?

    • http://trevorsullivan.net/ Trevor Sullivan

      You would have to adjust the script slightly to accomplish that. You could also use it as-is, and then use a text editor to remove the lines that do not match your expression.

      This is not a full-feature example, but just something to give people ideas.

      Cheers,
      Trevor Sullivan