66

In MacOSX there's a command which can pipe the output of a command to the clipboard so that it can be pasted somewhere else in the GUI.

How can this be done from cmd.exe or with a PowerShell cmdlet?

8 Answers 8

94

Use something like:

someCommand | clip

That will pipe the result to the windows clipboard

6
  • 1
    Good suggestion. This is a standard Windows utility. On most systems, it's at C:\Windows\system32\clip.exe. Sep 11, 2012 at 3:48
  • @NicoleHamilton right, but since that is part of the PATH variable, that does not need to be made explicit
    – soandos
    Sep 11, 2012 at 3:50
  • 1
    My point and part of the reason I gave you an upvote was that I liked that you'd suggested a standard Windows utility, not something the OP was supposed to d/l from somewhere. Most people know about PATH. Sep 11, 2012 at 4:25
  • 6
    For posterity, if you want the content of a file it's 'type somefile.txt | clip' Feb 2, 2014 at 21:10
  • 2
    Annoyingly windows echo adds a \r\n and has no echo -n ... so "echo test | clip" will have test\r\n in copy buffer ... echo|set/p="test"|clip ... addresses this gross
    – hpavc
    Feb 11, 2015 at 4:11
11

I'm using the Git Bash command shell for Windows, and as someone noted above, using clip is very annoying, because it also copies the carriage return at the end of the output of any command. So I wrote this function to address it:

function cpy {
while read data; do     # reads data piped in to cpy
    echo "$data" | cat > /dev/clipboard     # echos the data and writes that to /dev/clipboard
done
tr -d '\n' < /dev/clipboard > /dev/clipboard     # removes new lines from the clipboard
}

So for example:

$ pwd | cpy  # copies directory path

$ git branch | cpy # copies current branch of git repo to clipboard
1
  • 1
    Good one. If you have GNU win32 utils installed you could trim out the line break with tr and still use clip, i.e: pwd | tr -d \r\n | clip
    – pronebird
    Apr 18, 2018 at 10:07
7

In PowerShell, just pipe the text into Set-Clipboard. For fast typing, you can use the alias scb. This doesn't add an extra line break like the clip utility does.

For example, this command puts the contents of myfile.txt on the clipboard:

gc .\myfile.txt | scb

Note that for objects that represent file system objects, Set-Clipboard will copy the object in the same sense that Explorer does when you Ctrl+C a file. If that's not what you wanted, pipe the object through Out-String first.

1
  • 1
    This is the most useful Powershell-specific answer IMO, and is cross-platform; i.e. works with pwsh on Mac, etc.
    – Eric Pohl
    Feb 2, 2021 at 21:14
4

Just for reference I had to copy my public key directly after Bitbucket was giving me a bad key warning. I was able to use @soandos answer like so:

cat ~/.ssh/id_rsa.pub | clip to copy my key directly from the command line on a PC. (since command line sucks compared to terminal)

2

Geoffrey Huntley originally wrote the pasteboard project for Windows which includes the pbcopy and pbpaste commands. The project is now maintained by Mingxiang Xue at https://github.com/uzxmx/pasteboard on GitHub.

You can download the binaries from the latest release or install the binaries with scoop:

scoop bucket add extras
scoop install pasteboard

Then the pbcopy and pbpaste commands will be available as you are used on macOS.

1
1

This function replaces the standard Windows clip in Git Bash where a trailing newline is copied.

function clip {
    printf "$(</dev/stdin)" | cat > /dev/clipboard
}

This is based on Matthew's answer which only preserves the last line and introduces a trailing newline. Using printf avoids the newline that echo adds.

1

For use in WSL/bash. Add to ~/.bashrc:

function pbcopy() {
    printf $(</dev/stdin) | clip.exe
}
export -f pbcopy
1
  • Shouldn't that be printf %s "$(... to prevent % expansion in the input? I'm not sure why the printf is needed
    – w00t
    Nov 26, 2019 at 20:03
0

On Windows 10 and 11 add a couple of Aliases to your $profile on powershell to have a similar pbcopy and pbpaste commands OSX has.

Usually, should be on the following path:

  • powershell 7: C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1.
  • powershell 5: C:\Users\<username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

So add to the end of the $profile the following:

New-Alias pbpaste Get-Clipboard
New-Alias pbcopy Clip 

Then you can use pbcopy and pbpaste as you do on OSX:

echo "Hello World" | pbcopy
pbpaste
> Hello World!
pbpaste > a.txt
cat a.txt
> Hello World!

Note: On PowerShell cat is an alias of Get-Content. So, feel free to use either cat or Get-Content

If you prefer to use the commands as they are without aliases you can always run that as:

echo "Hello World" | clip
Get-Clipboard
> Hello World!
Get-Clipboard > a.txt
Get-Content a.txt
> Hello World!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .