≡ Menu

Powershell: Script to copy powershell command output to clipboard

From long time I have been thinking to have a powershell function which captures the output of a function/command and sends it to windows clipboard so that I can paste it where ever I want. But I failed to do that from time to time due to other commitments. Recently I came across a post from powershell.com which talked about the same topic. You can view the post at http://powershell.com/cs/blogs/tips/archive/2012/01/03/sending-text-to-clipboard-everywhere.aspx. I went through this post multiple times to understand what it is doing. Honestly, I am not good with windows forms or for that matter using dotnet classes from powershell. This code made me curious enough to read and understand windows forms as it used a System.Windows.Forms.TextBox class in it. I wondered why we need to use Textbox class to copy something from console to clipboard. After spending decent enough time, it turned out that the author of that function just wanted to utilized the Copy() function of that class which helps you to copy something to clipboard.

After understanding the code better, I felt usage of textbox class is somewhat unnecessary when dotnet providing separate classes to do clipboard operations. I continued my readings on bringing dotnet classes to powershell and finally came up with below code which copies the output of a cmdlet/function to clipboard.

You might want to ask, why I have to write the code again when I have something working. Well, there is nothing wrong with the code, I can happily use it. But if that requires expansion it terms of copying images to clipboard, handling data text formats, etc, that code is not efficient as TextBox doesn’t provide any methods to perform that. Given these reasons, I rewrote the function using Windows.Forms.Clipboard so that this can be expanded to perform variety of clipboard operations. Read http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx to understand different activities you can perform using this clipboard class.

Another thing is that, the function given by powershell.com is not porting the command executed to the clipboard which I feel valid because I(or people viewing the output) should know to what command the output belongs.

Well here is the modified version of Set-Clipboard function and hope you like this.

function Set-Clipboard {            
        Sets the system clip board with either output of a command/function/cmdlet or given text

        This function takes the $input from pipeline and sends it to clipboard so that we can paste the output wherever we want.
    .Parameter Text
        Text string that you want to store in clipboard
        Example 1:
        Get-Process | Set-Clipboard
        Example 1:
        Set-Clipboard -Text "Copy this string to clipboard"
        NAME:        Set-ClipBoard
        AUTHOR:        Sitaram Pamarthi
        WEBSITE:    https://techibee.com
 param (            

 if($text) {            
  $Clipboardtext = $text            
 } else {            
  $prompt = prompt            
  $clipboardtext = $prompt + $($myinvocation.line) + $($input | out-string)            
 $null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")             

PS C:\> Get-Process | Set-Clipboard

PS C:\>Set-Clipboard -Text “Copy me to clipboard”

Hope this helps and comments are welcome.




Comments on this entry are closed.