PowerShell Clipboard Cheat Sheet: Get-Clipboard and Set-Clipboard
To copy text to the clipboard in PowerShell, pipe it to Set-Clipboard (alias scb), for example Get-Content .\notes.txt -Raw | Set-Clipboard. To read the clipboard, run Get-Clipboard (alias gcb), adding -Raw if you want one string instead of an array of lines. Which other parameters you get depends on your version: Windows PowerShell 5.1 can also copy files, HTML and images, while PowerShell 7 handles text only.
Which PowerShell are you running?
Windows ships with Windows PowerShell 5.1 (powershell.exe). PowerShell 7 (pwsh.exe) is a separate install. The clipboard cmdlets differ a lot between them, so check before copying commands from the internet:
$PSVersionTable.PSVersion
A major version of 5 means Windows PowerShell 5.1. A 7 means PowerShell 7.
Quick reference: 5.1 vs 7
| Task | Windows PowerShell 5.1 | PowerShell 7.4 and later |
|---|---|---|
| Copy text | Set-Clipboard -Value "text" | Same |
| Add to the current text | Set-Clipboard -Value "more" -Append | Same |
| Clear the clipboard | Set-Clipboard -Value $null | Same |
| Copy files so Explorer pastes them | Set-Clipboard -Path 'C:\Dir\*' | Not available |
| Copy formatted HTML | Set-Clipboard -Value $html -AsHtml | Not available |
| Set your local clipboard over SSH | Not available | Set-Clipboard -Value "x" -AsOSC52 |
| Read text as an array of lines | Get-Clipboard | Same |
| Read text as one string | Get-Clipboard -Raw | Same |
| Split on a custom delimiter | Not available | Get-Clipboard -Delimiter "," (7.6 and later) |
| List copied files | Get-Clipboard -Format FileDropList | Not available |
| Get a copied image | Get-Clipboard -Format Image | Not available |
| Get the HTML or RTF version | Get-Clipboard -TextFormatType Html | Not available |
-Format accepts Text, FileDropList, Image and Audio. -TextFormatType accepts Text, UnicodeText, Rtf, Html and CommaSeparatedValue. Both exist only in 5.1, as do -Path, -LiteralPath and -AsHtml.
Copying text with Set-Clipboard
The most common form is piping something into it:
Set-Clipboard -Value "This is a test string"
Get-Content C:\Users\user1\.ssh\id_ed25519.pub | Set-Clipboard
The second line is Microsoft's own example: put a public SSH key on the clipboard, then paste it into GitHub.
Things to know about how the text lands:
- Several strings are joined with line breaks. When you pipe a file line by line, the lines are joined with a newline and no newline is added at the end. Use
Get-Content -Rawif you need the file exactly as it is, including its final line break. -Appendadds a line break first. The new text goes after a newline, below whatever is already on the clipboard.- Objects aren't text. Convert them first with
Out-String. Without it, Windows PowerShell 5.1 can bind an object'sPSPathproperty to-Pathand copy a file instead. For example,Get-ChildItem *.log | Set-Clipboardputs the log files themselves on the clipboard, not a list of names.
Get-Process | Select-Object -First 10 | Format-Table | Out-String | Set-Clipboard
Get-ChildItem *.log | Select-Object -ExpandProperty FullName | Set-Clipboard
The first line copies a formatted table. The second copies the full paths as text, one per line. Note that Out-String ends its output with a line break; call .Trim() on the result if the paste target cares. If you only need a path, how to copy a file path that works in the terminal covers quoting and spaces.
Reading the clipboard with Get-Clipboard
Get-Clipboard returns an array of strings, one per line, like Get-Content. That surprises people who expect one string:
$lines = Get-Clipboard # array, one element per line
$text = Get-Clipboard -Raw # one multi-line string
(Get-Clipboard -Raw) -match 'ERROR'
Use -Raw whenever you compare, parse or search the whole clipboard. For example, to reformat minified JSON copied from a browser:
Get-Clipboard -Raw | ConvertFrom-Json | ConvertTo-Json -Depth 10 | Set-Clipboard
In PowerShell 7.6 and later, -Delimiter controls how the text is split. The default on Windows is a CRLF line break, so text that uses LF-only line endings (common when copying from WSL or a Linux server) comes back as one element. This splits on either:
Get-Clipboard -Delimiter "`r`n", "`n"
On older versions, (Get-Clipboard -Raw) -split '\r?\n' does the same job.
Copying files and images (Windows PowerShell 5.1)
These need the 5.1 parameters. Copy files so that Ctrl+V in Explorer pastes real files:
Set-Clipboard -Path 'C:\Staging\*'
Set-Clipboard -LiteralPath 'C:\Reports\Q3 [final].pdf'
-Path accepts wildcards. -LiteralPath takes the path exactly as written, which matters when a name contains square brackets. To read files someone copied in Explorer:
Get-Clipboard -Format FileDropList | Select-Object FullName, Length
To save a copied screenshot (for example from Win+Shift+S) as a PNG file:
Add-Type -AssemblyName System.Drawing
$img = Get-Clipboard -Format Image
if ($img) { $img.Save('C:\Temp\clip.png', [System.Drawing.Imaging.ImageFormat]::Png) }
$img is empty when the clipboard holds no image, so the if avoids an error. The difference between copying a file and copying its contents is explained in files vs file contents, and how Windows file drop lists work covers what Explorer actually puts on the clipboard.
The same jobs from PowerShell 7
PowerShell 7 can't do these directly, but Windows PowerShell 5.1 is still on every Windows 10 and 11 PC. Call it from pwsh:
powershell.exe -NoProfile -Command "Set-Clipboard -LiteralPath 'C:\Reports\q3.pdf'"
powershell.exe -NoProfile -Command "Get-Clipboard -Format FileDropList | Select-Object -ExpandProperty FullName"
powershell.exe -NoProfile -Command "Get-Clipboard -TextFormatType Html"
Copying formatted HTML (5.1)
-AsHtml puts your content on the clipboard as HTML, so rich editors such as Word or Outlook can paste it formatted rather than as literal tags:
$html = '<b>Build passed</b> on <i>main</i>'
Set-Clipboard -Value $html -AsHtml
The reverse is Get-Clipboard -TextFormatType Html, which returns the HTML version of what you copied, such as a table from a web page. How Windows treats rich text versus plain text explains why one copy carries several formats.
Multi-line text with here-strings
A here-string is the cleanest way to put a block of code or text on the clipboard without fighting quotes:
$snippet = @'
function Get-Hash {
param([string]$Path)
Get-FileHash -Path $Path -Algorithm SHA256
}
'@
Set-Clipboard -Value $snippet
The rules, from Microsoft's quoting documentation:
- The opening mark (
@'or@") must be followed by a newline. - The closing mark (
'@or"@) must come after a newline, at the start of its line. - The final newline before the closing mark belongs to the mark, so it isn't part of the string.
@'...'@is verbatim:$Pathstays as typed. Use it for code, especially code in another language that also uses$.@"..."@expands variables and$(...)subexpressions, like any double-quoted string.
The line breaks inside a here-string are the ones in your source. A script saved with LF line endings produces LF text, which some Windows apps display as one long line. Normalise before copying if that matters:
Set-Clipboard -Value ($snippet -replace '\r?\n', "`r`n")
Going the other way, convert to LF before pasting clipboard text into bash or a strict parser:
(Get-Clipboard -Raw) -replace "`r`n", "`n"
WSL and the Windows clipboard covers line endings between WSL and Windows in more detail.
Copying over SSH with -AsOSC52 (PowerShell 7.4+)
In an SSH session, Set-Clipboard normally sets the clipboard of the remote machine. -AsOSC52 instead sends the OSC 52 escape sequence, which your terminal uses to set your local clipboard. The terminal has to support it; Windows Terminal does.
Set-Clipboard -Value "This is a test string" -AsOSC52
To make it automatic in SSH sessions, Microsoft suggests adding one of these lines to your PowerShell profile:
$PSDefaultParameterValues['Set-Clipboard:AsOSC52'] = $Env:SSH_CLIENT
$PSDefaultParameterValues['Set-Clipboard:AsOSC52'] = $Env:SSH_TTY
If copied terminal output arrives with stray escape codes, see how to copy from Windows Terminal without garbled text.
The clip.exe alternative
clip is a Windows command that sends whatever is piped into it to the clipboard. It works in Command Prompt, batch files and PowerShell:
ipconfig | clip
In Command Prompt, clip < readme.txt copies a file's contents. clip is write-only; there is no matching command to read the clipboard, so use Get-Clipboard for that.
Two differences from Set-Clipboard when you use it from PowerShell:
- It gets a trailing line break. PowerShell ends piped text with a newline, so
"text" | clipcopies the text plus a line break. - Encoding depends on
$OutputEncoding. PowerShell converts text to bytes using that variable beforeclipsees it, and its documented default in Windows PowerShell 5.1 is ASCII. Accented letters, emoji and other non-ASCII characters can be lost.Set-Clipboarddoesn't go through that conversion.
Common mistakes
- Comparing
Get-Clipboardto a string without-Raw. You're comparing an array. - Piping objects straight to
Set-Clipboard. UseOut-Stringor select the property you want. - Expecting
clipto be an alias forSet-Clipboard. It's a separate program. The aliases arescbandgcb. - Copying a 5.1 example into PowerShell 7.
-Format,-TextFormatType,-Pathand-AsHtmldon't exist there. - Calling
Set-Clipboardmany times in a tight loop. Microsoft notes it can occasionally fail with an ExternalException while the clipboard is busy; a shortStart-Sleep -Milliseconds 100between calls helps. - Copying secrets in scripts. Anything you
Set-Clipboardcan end up in Win+V history or a clipboard manager. Is an API key on the clipboard a security incident? covers what to do instead.
Clearing the clipboard with Set-Clipboard -Value $null empties the current clipboard only. Items already saved in Win+V history stay there until you clear clipboard history.
FAQ
How do I copy the output of a command to the clipboard in PowerShell?
Pipe it through Out-String and into Set-Clipboard, for example Get-Service | Out-String | Set-Clipboard. For plain text output, | clip also works.
Does Get-Clipboard work in PowerShell 7 on Linux or macOS?
Yes, for text. On Linux it needs the xclip utility on your path; on macOS it uses pbpaste (and Set-Clipboard uses pbcopy).
Why doesn't Get-Clipboard return my screenshot or copied files?
By default it returns text only. An image, a file list or the formatted version of rich text needs -Format or -TextFormatType, which exist only in Windows PowerShell 5.1.
Related reading
- Windows Terminal: Copy Plain Text Without Formatting
- How to Copy a File Path That Works in the Terminal
- WSL Clipboard: Copy and Paste Between Linux and Windows
- AutoHotkey v2 Clipboard Scripts: 5 Copy-Paste Examples
Sources
- Set-Clipboard, PowerShell 7.6 (Microsoft Learn) — parameters, -Append behaviour, -AsOSC52 (added in 7.4), SSH profile example, alias scb, Linux and macOS requirements.
- Set-Clipboard, Windows PowerShell 5.1 (Microsoft Learn) — -Path, -LiteralPath, -AsHtml, and the note on rapid repeated calls.
- Get-Clipboard, PowerShell 7.6 (Microsoft Learn) — -Raw, -Delimiter and its default, alias gcb.
- Get-Clipboard, Windows PowerShell 5.1 (Microsoft Learn) — -Format and -TextFormatType values and output types.
- SetClipboardCommand.cs (PowerShell on GitHub) — how multiple strings are joined and how empty input clears the clipboard.
- about_Quoting_Rules (Microsoft Learn) — here-string syntax and the final-newline rule.
- Out-String (Microsoft Learn) — default output includes trailing newlines.
- about_Preference_Variables, 5.1 (Microsoft Learn) — $OutputEncoding and its ASCII default.
- clip (Microsoft Learn) — syntax and examples.
Deepender Yadav created Edge Drop and builds and tests it, from the edge-hover shelf to each release. A B.Tech Computer Science Engineering student, Deepender writes the Edge Drop guides, comparisons and developer posts, and checks product details against the app's source code.
GitHub · LinkedInCopy. Stack. Drop.
Transform your clipboard into an interactive edge shelf. Stack, pin, and drag assets into any app with zero friction.
Download for Windows Get from Microsoft Store
How to Install Guide · First 10 Minutes Guide · Drag & Drop Guide · Edge Drop vs Win+V · Support
Free · Lightweight · Privacy First