Post

PowerShell Get-Clipboard: The Windows Alternative to Mac's pbpaste

Learn about PowerShell's Get-Clipboard command - the Windows equivalent to Mac's pbpaste for clipboard management.

As a PowerShell enthusiast, I thought I knew all the essential clipboard tricks. I’ve been using the clip command for years to redirect CLI output straight to my clipboard:

1
dir temp | clip

Simple, effective, and a real time-saver. But today, while watching a YouTube video of someone building a development tool, I discovered something that made me pause the video and immediately open my terminal.

The Mac Approach

The presenter was working on a Mac and used this command:

1
pbpaste > mynewfile

This redirects clipboard content directly into a new file - a useful technique for quickly capturing and manipulating clipboard content in scripts and workflows.

I must admit I was a bit jealous of this command. My typical workflow involved copying content, pasting it into a file, and then working with that file for redirection. This single command eliminated multiple steps and was much more elegant. I had never seen anything like this before, nor had I seen anyone else use a similar technique.

The Windows Equivalent

This made me wonder, does PowerShell have something similar?

It does. PowerShell provides the same functionality through a built-in command. The command is Get-Clipboard - which just rolls off the tongue, right? Thankfully, it also has a short alias gcb:

1
2
3
4
5
# The full command
Get-Clipboard

# The short alias
gcb

Practical Examples

Basic Clipboard Retrieval

1
2
3
4
5
6
7
8
# Get whatever is currently in your clipboard
gcb

# Redirect clipboard content to a file (just like pbpaste!)
gcb > mynewfile.txt

# Append clipboard content to an existing file
gcb >> logfile.txt

PowerShell Pipeline Power

Since this is PowerShell, you get all the pipeline goodness:

1
2
3
4
5
6
7
8
9
10
11
# Count lines in clipboard content
gcb | Measure-Object -Line

# Search for specific text in clipboard
gcb | Select-String "error"

# Process clipboard content and save filtered results
gcb | Where-Object { $_ -like "*important*" } > filtered.txt

# Convert clipboard JSON to PowerShell objects
gcb | ConvertFrom-Json

Workflow Examples

Scenario 1: Quick Log Analysis

Copy some log content from a web interface, then:

1
gcb | Select-String "ERROR|WARN" | Out-File errors.log

Scenario 2: Code Snippet Management

1
2
# Save a code snippet from clipboard
gcb > "snippets\$(Get-Date -Format 'yyyy-MM-dd-HHmm').ps1"

Scenario 3: Data Processing

1
2
# Process CSV data copied from Excel
gcb | ConvertFrom-Csv | Where-Object Status -eq "Active"

The Complete Clipboard Toolkit

Now you have the full Windows clipboard command:

DirectionCommandExample
To Clipboardclipdir \| clip
From ClipboardGet-Clipboard or gcbgcb > file.txt

Additional Resources

This post is licensed under CC BY 4.0 by the author.