Post

How to use Pbcopy and Pbpaste commands from MacOS on Windows

Use the popular `pbcopy` and `pbpaste` commands from MacOS on PowerShell

How to use Pbcopy and Pbpaste commands from MacOS on Windows

Introduction

If you have a background of using MacOS you probably have used the commands pbcopy and pbpaste.

Can you use something similar on Windows?

You can, and there are a few options.

  • clip.exe, this old command only works one way.
  • Set-Clipboard and Get-Clipboard on Powershell.
  • pasteborad — OSX pbcopy and pbpaste clone.

The basic operations for pbcopy and pbpaste on macOS are

pbcopy

1
cat hello-world.txt | pbcopy

pbpaste

1
2
3
4
5
pbpaste > hello-world.txt

# or to print out to stdout

pbpaste

As for Windows these operations can be replicated easily in several ways. I will try to explain all the ones I know.

How To on Windows

clip.exe

The old clip.exe command should be available by default on Windows, it has been around since MSDOS and can be either used on CMD or Powershell

1
2
3
where.exe clip

# C:\Windows\System32\clip.exe

For example to append to your clipboard a file

1
Get-Content hello-world.txt | clip

This only works one way though, you can’t use it to send the output or paste

Finally you can add an alias on your Powershell $PROFILE by adding this

1
Set-Alias pbcopy Clip

And for the pbpaste you can just use the Powershell new function

1
Set-Alias pbpaste Get-Clipboard

Set-Clipboard and Get-Clipboard (Powershell)

I have briefly introduced before Get-Clipboard previously but if you use it in conjunction with Set-Clipboard, you can achieve the same as if you were using pbcopy and pbpaste.

To add text to your clipboard from a file

1
Get-Content hello-world.txt | Set-Clipboard

And to use the clipboard and redirect it into a file

1
Get-Clipboard > hello-world.txt

Then to set these to be a clone of pbcopy and pbpaste just add these aliases to your $PROFILE

1
2
Set-Alias pbcopy Set-Clipboard
Set-Alias pbpaste Get-Clipboard

Pasteboard

Install it using scoop

1
2
scoop bucket add extras
scoop install extras/pasteboard

Then you get pbcopy and pbpaste tools out of the box

1
type hello-world.txt | pbcopy
1
pbpaste > hello-world.txt
This post is licensed under CC BY 4.0 by the author.