Windows 7 comes with PowerShell 2.0 preinstalled and you can use it to manipulate the clipboard directly with Windows.Forms.Clipboard
(or Windows.Clipboard
in PresentationCore). Some examples
Add-Type -AssemblyName PresentationCore
[Windows.Clipboard]::GetText()
$out = "some text"
[Windows.Forms.Clipboard]::SetText($out)
[Windows.Clipboard]::GetData([Windows.DataFormats]::UnicodeText)
[Windows.Clipboard]::GetData([Windows.DataFormats]::Html)
if ([Windows.Clipboard]::ContainsFileDropList()) {
$f = [Windows.Clipboard]::GetFileDropList()
Write-Host $f
}
if ($out)
{
[Windows.Clipboard]::SetText($out);
}
else
{
[Windows.Clipboard]::Clear();
}
The first line can be changed to Add-Type -AssemblyName System.Windows.Forms
along with some replacements:
[Windows.Clipboard]
→ [Windows.Forms.Clipboard]
[Windows.DataFormats]
→ [Windows.Forms.DataFormats]
You can even write a PowerShell script with GUI to view and edit the clipboard. Fortunately there's an already-made solution here: Building a Clipboard History Viewer Using PowerShell
I saw a question a while back in the Technet PowerShell Forum asking how one might start to build a clipboard viewer using PowerShell that met a few requirements:
- Have an open window aside from the PowerShell console
- Automatically list new clipboard items as they come in
- Allow for filtering to find specific items
You can also download the script directly from MS Technet Gallery. Note that it only supports text so you'll have to modify it if you want to include other data formats like images or files
Another solution for PowerShell 2.0 is the module ClipboardText which can be installed with Install-Module -Name ClipboardText
Set-ClipboardText "some text"
Get-ClipboardText
It's also available on GitHub
If you have access to PowerShell 5.0 and up, you can use the built-in cmdlets Get-Clipboard
and Set-Clipboard
directly. They support not only text but also other objects in clipboard. Some examples
Get-Clipboard -Format Text -TextFormatType Html
Get-Clipboard -Format FileDropList
(Get-Clipboard -Format FileDropList)[0].GetType()
See also New Stuff - Get-Clipboard And Set-Clipboard - New In PowerShell 5.0
There also various portable applications for clipboard manipulation