| by Arround The Web | No comments

Using PowerShell Copy to Clipboard Function

Normally, in Windows the text or content is copied to the clipboard by pressing the “CTRL+C” shortcut key. However, in PowerShell, the text can also be copied to the clipboard using the “Set-Clipboard” cmdlet. This cmdlet sets the content to the clipboard. Moreover, the copied cmdlets can be pasted too in PowerShell using the “Get-Clipboard” cmdlet.

The following post will elaborate on the method to copy the content to the clipboard.

Using PowerShell Copy to Clipboard Function

As described earlier, the copy to clipboard function in PowerShell sets the text or content to the clipboard. The command used for that purpose is the “Set-Clipboard”. Examples explaining the procedure to copy the text to the clipboard are explained below.

Example 1: Copy the Text to the Clipboard Using the “Set-Clipboard” Cmdlet

The following illustration will demonstrate the procedure to copy the stated text into the clipboard:

Write-Output "Welcome to Linux Hint!" | Set-Clipboard

In the above-stated code:

  • First, specify the “Write-Output” cmdlet followed by the text within inverted double quotes.
  • Then, add the “|” pipeline parameter to make the previous cmdlet’s output be sent to the next.
  • Lastly, add the “Set-Clipboard” cmdlet:

Execute the below cmdlet to verify whether the content was copied to the clipboard or not:

Get-Clipboard

Example 2: Append the Text to the Existing Clipboard

To append the content or text to the existing copied content simply add the “-Append” parameter at the end of the code line:

Write-Output "Append this text" | Set-Clipboard -Append

Verify whether the content was copied to the clipboard or not by executing the given cmdlet:

Get-Clipboard

Example 3: Copy the Variable Assigned Content to the Clipboard

The following example will demonstrate the procedure to copy the variable assigned text to the clipboard:

$greetings = "Good luck!"
Set-Clipboard -Value $greetings

According to the above code:

  • First, initialize a variable and assign the text to it.
  • Then, in the next line write the “Set-Clipboard” cmdlet.
  • After that, add the “-Value” parameter and assign the text assigned variable:

Execute the “Get-Clipboard” cmdlet to verify whether the content was copied to the clipboard or not:

Get-Clipboard

That was all about copying items to the clipboard.

Conclusion

The “Set-Clipboard” cmdlet in PowerShell is utilized to copy the content or text to the clipboard. It can set the text or variables to the clipboard by using some specific parameters. Moreover, the copied text can be pasted using the “Get-Clipboard” cmdlet. This post has elaborated on the process to copy or set the content to the clipboard in PowerShell.

Share Button

Source: linuxhint.com

Leave a Reply