How to Use the Get-Clipboard Output in PowerShell
This article demonstrates how we can use the content of our clipboard in Windows PowerShell. You have probably been using the common Ctrl+C keyboard shortcuts for copying content.
This article will discuss how you can manipulate the content you have copied.
Use the Get-Clipboard
Command and Use Its Output in PowerShell
We use the Get-Clipboard
cmdlet to display the content of our clipboard on PowerShell. Here is an example:
We will copy some random text from a webpage and output the same on Powershell. Below is the script we will use:
Get-Clipboard
It is important to note that from PowerShell core V6+ the only supported data format is .text
. You can check your PowerShell version by running the $PSVersionTable
command.
How can we use the content of our clipboard in PowerShell? Here is a simple way.
Let’s say we have copied the text and want to paste it into a file on our machine. How would we go about this using PowerShell?
We will employ the Add-Content
cmdlet on Powershell in such a scenario. Here is the script we will use:
Add-Content -Path C:\Users\pc\Demo\Demo.txt -Value (Get-Clipboard)
This should paste the contents of our clipboard to our Demo.txt
file. Let’s try something more complicated.
Let’s say we have a Demo.txt
file in the Demo
folder, and we want to make a copy of the file in the Trial
folder. How do we go about this using PowerShell?
In such a scenario, we will use the Copy-Item
cmdlet to copy the Demo.txt
file, and for the Destination
parameter, we will use the full path of our Trial
folder.
For that to happen, we will need to copy the full path of our Trial
folder. We will use the Shift key and the right mouse button to copy the path.
This will get the full path of the Trial
folder as a quoted path string.
Then we will run the script below:
Copy-Item C:\Users\pc\Demo\Demo.txt -Destination (Get-Clipboard).Replace('"', "")
Note that Copy-Item
rejects quoted paths. Our remedy to that is replacing the quotes hence the .Replace('"',"")
part in the script.
In a nutshell, the Get-Clipboard
cmdlet allows us to manipulate the contents of our clipboard. Remember that since PowerShell core v6+, only text
data is supported.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn