{"id":355,"date":"2021-05-01T05:00:59","date_gmt":"2021-05-01T12:00:59","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=355"},"modified":"2021-05-06T09:53:04","modified_gmt":"2021-05-06T16:53:04","slug":"sending-data-to-the-clipboard-from-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/sending-data-to-the-clipboard-from-powershell\/","title":{"rendered":"Sending data to the Clipboard from PowerShell"},"content":{"rendered":"<p><strong>Q:<\/strong> Hey I have a fun question! I remember reading a while back about using VBScript to paste to the clipboard. Are we able to do that with PowerShell?<\/p>\n<p><strong>A:<\/strong> Why yes, yes we can! It is far often a much quicker solution if we start with PowerShell!<\/p>\n<h2>Pasting content to the clipboard, the old VBScript method<\/h2>\n<p>Before we show the quick and easy solution, let&#8217;s learn how we could adapt an older solution.<\/p>\n<p>Now back in the day if I wanted to paste something on the clipboard I would go down to the store, get some glue and&#8230;<\/p>\n<p>&#8220;DOH! Wrong Clipboard!&#8221; (Knew I should have splashed some water on my face before typing this up!)<\/p>\n<p>What I should have said was back before PowerShell existed we actually had TWO methods to paste text data to the clipboard.<\/p>\n<p>One was a nice simple solution if you were working in DOS or has simple text output from a VBScript. You would pipe the output to the <code>clip<\/code> command as seen below<\/p>\n<pre><code class=\"language-output\">dir | clip<\/code><\/pre>\n<p>Once this was complete you could paste your captured text using a <code>CTRL-V<\/code> in whichever Windows application.<\/p>\n<p>Another method that presented itself was using some code such as this in VBScript<\/p>\n<pre><code class=\"language-VBScript\">strCopy = \"This text has been copied to the clipboard.\"\nSet objIE = CreateObject(\"InternetExplorer.Application\")\nobjIE.Navigate(\"about:blank\")\nobjIE.document.parentwindow.clipboardData.SetData \"text\", strCopy\nobjIE.Quit<\/code><\/pre>\n<p>So I could re-use this solution in PowerShell quite easily. I do this in case you might ever see some older VBScript that you might want to reuse.<\/p>\n<p>The first line in VBScript is assigning a string<\/p>\n<pre><code class=\"language-VBScript\">strCopy = \"This text has been copied to the clipboard.\"<\/code><\/pre>\n<p>In PowerShell I can do this in the following manner.<\/p>\n<pre><code class=\"language-powershell\">$strCopy = \"This text has been copied to the clipboard.\"<\/code><\/pre>\n<p>The next line is where a Comobject is created.<\/p>\n<pre><code class=\"language-VBScript\">Set objIE = CreateObject(\"InternetExplorer.Application\")<\/code><\/pre>\n<p>The equivalent code in PowerShell to do the same thing and even use the same variable name for the object would be<\/p>\n<pre><code class=\"language-powershell\">$objIE = New-Object -comobject \"InternetExplorer.Application\"<\/code><\/pre>\n<p>Then from this point the final lines are just manipulating data in the Object.<\/p>\n<pre><code class=\"language-VBScript\">objIE.Navigate(\"about:blank\")\nobjIE.document.parentwindow.clipboardData.SetData \"text\", strCopy\nobjIE.Quit<\/code><\/pre>\n<p>Which in PowerShell would look like this.<\/p>\n<pre><code class=\"language-powershell\">$objIE.Navigate(\"about:blank\")\n$objIE.document.parentwindow.clipboardData.SetData(\"text\", strCopy)\n$objIE.Quit<\/code><\/pre>\n<p>However if you try this solution in a modern version of Windows, it will appear to just sit and hang in PowerShell.<\/p>\n<p>We can one extra line to the original code and you see why.<\/p>\n<pre><code class=\"language-powershell\">$objIE.Navigate(\"about:blank\")\n\n# Show the hidden Internet Explorer background application\n$objIE.Visible=$True\n\n$objIE.document.parentwindow.clipboardData.SetData(\"text\", strCopy)\n$objIE.Quit<\/code><\/pre>\n<p>The following Window below demonstrates why the old solution, even when converted to PowerShell failed.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2021\/04\/InteractivePromptStoppingTheOldSolution.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2021\/04\/InteractivePromptStoppingTheOldSolution-300x217.png\" alt=\"Image InteractivePromptStoppingTheOldSolution\" width=\"300\" height=\"217\" class=\"alignnone size-medium wp-image-361\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2021\/04\/InteractivePromptStoppingTheOldSolution-300x217.png 300w, https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2021\/04\/InteractivePromptStoppingTheOldSolution.png 551w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>In fact, even if we just ran it in VBScript today, it would have failed in an equal manner.<\/p>\n<h2>The drawback to converting from VBScript<\/h2>\n<p>So that was pretty cool, you tried to re-use some VBScript to meet your task.<br \/>\nIn this case because security has improved in past 17 years, Internet Explorer is not allowed to just paste things to the clipboard.<\/p>\n<p>But although this is a nice way to learn how to convert over some older code from VBScript, it is actually not a good used of PowerShell for two reasons.<\/p>\n<ol>\n<li>It leverages Internet Explorer which, for this purpose, is a big resource to solve the problem at hand. We can also no longer automate in this manner.<\/li>\n<li>PowerShell has built in cmdlets to solve the problem which are far easier to use. They not only work well in Windows PowerShell, but also just as seamlessly across all supported Operating Systems when using PowerShell 7.x.<\/li>\n<\/ol>\n<h2>The clipboard cmdlets in PowerShell<\/h2>\n<p>You can verify they exist by just using <code>Get-Command<\/code> like the sample below<\/p>\n<pre><code class=\"language-output\">Get-Command *clipboard*\n\nCommandType     Name            Version    Source\n-----------     ----            -------    ------\nCmdlet          Get-Clipboard   7.0.0.0    Microsoft.PowerShell.Management\nCmdlet          Set-Clipboard   7.0.0.0    Microsoft.PowerShell.Management<\/code><\/pre>\n<p>To populate the clipboard with a directory structure, as an example, I can execute the following line<\/p>\n<pre><code class=\"language-output\">PS&gt; Get-Childitem | Set-Clipboard<\/code><\/pre>\n<p>There is no visual output because the data is now stored on the Clipboard.<\/p>\n<p>To verify this in PowerShell you can use the <code>Get-Clipboard<\/code> Cmdlet.<\/p>\n<pre><code class=\"language-output\">PS&gt; Get-Clipboard\nC:\\Demo\\AzureADBaseline\nC:\\Demo\\AzureDSC\nC:\\Demo\\AzureVM-Json\nC:\\Demo\\DualStateMitigate\nC:\\Demo\\testmoduleforme.ps1\nC:\\Demo\\TheShellofBlueness.docx<\/code><\/pre>\n<p>So yes, I wasted a bit of time showing you how to convert some VBScript. Please have pity on me for my intent was good. Shame on me. \ud83d\ude42<\/p>\n<h2>Summary<\/h2>\n<p>It is nice to know that you can convert over to PowerShell in a pretty simple manner from VBScript if you needed to. There are a lot of excellent examples of how to manage Windows environments with VBScript readily written.<\/p>\n<p>It is equally important to understand why we would choose to start fresh with the solution in PowerShell.<\/p>\n<p>It gives us a solution, in this case of manipulating the clipboard in an Operating System; which is consistent across the board whether you choose to use Windows, macOS, Linux or any supported Operating System for PowerShell 7.x.<\/p>\n<p>The choice is yours my friends!<\/p>\n<h2>Tip of the Hat<\/h2>\n<p>This article was based on one written back on older post on <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/can-i-copy-script-output-to-the-clipboard\/\">&#8220;Can I Copy Script Output to the Clipboard&#8221;<\/a><\/p>\n<p>I do not recall the author but it was a good way to learn how to programmatically set the clipboard back then. It was time to get it updated.<\/p>\n<p>Cheers all!<\/p>\n<p>Your friend in Automation<\/p>\n<p>Sean Kearney &#8211; Customer Engineer\/Microsoft &#8211; @PowerShellMan<\/p>\n<p><em>&#8220;Remember with great PowerShell comes great responsibility&#8230;&#8221;<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: Hey I have a fun question! I remember reading a while back about using VBScript to paste to the clipboard. Are we able to do that with PowerShell? A: Why yes, yes we can! It is far often a much quicker solution if we start with PowerShell! Pasting content to the clipboard, the old [&hellip;]<\/p>\n","protected":false},"author":51959,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[31,3,32],"class_list":["post-355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-clipboard","tag-powershell","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Q: Hey I have a fun question! I remember reading a while back about using VBScript to paste to the clipboard. Are we able to do that with PowerShell? A: Why yes, yes we can! It is far often a much quicker solution if we start with PowerShell! Pasting content to the clipboard, the old [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/51959"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}