Monday, June 9, 2014

#15 : Handling Clipboard with Powershell

Not sure when you might need to get data from Clipboard. But I thought it might be useful in some cases which I might not think about.
So, below is the piece of code which can be used.

Getting Data from Clipboard:

Below code will look if the Clipboard contains a text data and if this is so, it would display. You may store it into variable and do whatever you like.

#--Statement to Get Text Data from Clipboard --#   
if ( [System.Windows.Forms.Clipboard]::GetText() -ne ""  )    
{    
    [System.Windows.Forms.Clipboard]::GetText()   
   
else   
{   
    Write-Output "Clpboard does not contain Text data"    
}    


Setting Data into Clipboard:

Use below piece of code to set a value to the Clipboard. To confirm if value is set or not, please use above section.

#--Statement to Set item into Clipboard --#   
$Data_to_Set="Som DT"    
if ($Data_to_Set -ne "" )    
{    
    [System.Windows.Forms.Clipboard]::SetText($Data_to_Set)   
}   



Clearing Data from Clipboard:

Below code can be used to clear data available in clipboard.

#--Statement to clear the data from clipboard --#    
[System.Windows.Forms.Clipboard]::Clear()   


I hope this will help you. Please comment here if you have any thoughts about it.

3 comments:

  1. Thank you for sharing this helped me out!

    Really wish there was a cmdlet for managing the clip board built in.

    ReplyDelete
  2. Just what I was looking for, thanks.

    ReplyDelete
  3. When I save the Getting Data from Clipboard: above as a ps1 file, it does nothing. It shows no output. What I want to do is copy the clipboard (a url)and send it to vlc media player. This is what a functional command line looks like in a .bat file: "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "https://playerservices.streamtheworld.com/api/livestream-redirect/KINK.mp3" but I don't know how to replace the url in that command line with the current clipboard contents.

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...