Can I Copy Script Output to the Clipboard?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way to have my script output copied to the Clipboard?

-- ZW, Marseilles, France

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZW. Well, as long as you don’t mind crazy work-arounds then, yes, it’s actually pretty easy to copy script output to the Clipboard. To start with, you need to construct a string that contains the desired output. After that, you create an instance of Internet Explorer, and open it to a blank page. You can then take advantage of the capabilities built into the Internet Explorer object model to copy the string to the Clipboard; in particular, you use the clipboardData.SetData method to accomplish this trick. A simple script to copy something to the Clipboard looks like this:

strCopy = "This text has been copied to the clipboard."

Set objIE = CreateObject("InternetExplorer.Application")
objIE.document.parentwindow.clipboardData.SetData "text", strCopy

Run the script, then open up Notepad and click Paste; you should see your string in all its glory.

By the way, all of this happens “under the covers;” Internet Explorer won’t actually pop up on your screen. That’s because, by default, any instance of IE you create from a script runs hidden, at least until you do something like this:

objIE.Visible = True

For more information about clipboardData.SetData, see the DHTML reference documentation on MSDN.

Comments (14)

  1. Anonymous says:

    Hey thanks, Ed! I learned something new today!

  2. Anonymous says:

    Hello, Script Guy!

    I want to send my highlighted lines to the ClipBoard.

    I want to do it as one of my steps inside a VBS script.

    I cannot  use the common Ctrl+C.


    Is it at all possible ?

    Manfred Lutz.


  3. T Luke says:

    Is there a way to do this without IE?

  4. Ed Wilson says:

    @T Luke you can use the clip.exe program that is part of the operating system. In Windows PowerShell it is as simple as this:

    "Copy this string to the clipboard" | clip

    Using VBScript, you will need to create the wshshell object and use the exec method to start clip, and then play around with stdin to get your input. But it can be done … Nowdays, attempting to automate IE is a hit or miss proposition due to all the security changes — this particular article was written more than 7 years ago.

  5. Ed Wilson says:

    @JeremyEngleWork you are welcome.

  6. benj9 says:

    Instead of the IE I like it more to call a HTA to set the clipboard. It's fast and reliable and does not spam us with a commit dialog.

    Here an example script. The function 'SetClipboard' itself just creates a temporary HTA (if it's not already there, and passes the new clipboard content by command line argument.

    Set oFs = CreateObject("Scripting.FilesystemObject")

    Set oWsh = CreateObject("WScript.Shell")

    SetClipboard "Hello Clipboard"

    WScript.Echo GetClipboard()


    Sub SetClipboard(sText)

    Dim sFile, oFile

    sFile = oWsh.ExpandEnvironmentStrings("%TEMP%")

    If sFile = "%TEMP%" Then sFile = "."

    sFile = sFile & "~SetClipboard.hta"

    WScript.Echo sFile

    If Not oFs.FileExists(sFile) Then

    Set file = oFs.CreateTextFile(sFile, True)

    file.WriteLine "<HTA:APPLICATION id=c APPLICATIONNAME=/><SCRIPT language=VBScript>" _

    & "document.parentwindow.clipboardData.SetData ""text""," _

    & "Right(C.commandLine,Len(c.commandLine)-InStr(2,c.commandLine,Chr(34))-1):window.close" _


    file.Close : Set file = Nothing

    End If

    oWsh.Run sFile & " " & sText, 0, True

    End Sub

    Function GetClipboard()

    GetClipboard = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

    End Function

  7. Nerunja says:

    Hi benj9, That was really cool. Thanks alot friend. I tried and it worked awesome..

  8. j says:


  9. venkata sailendra says:

    I could not run this script
    Could you please share the steps to run it

  10. venkata sailendra says:

    Hi sir ,
    please help me to run this script.I am new to this.

  11. terry says:

    Don’t know why but it doesn’t work for me.

  12. JODww says:

    It didn’t work for me first off but if you remove the carriage returns from this section it should be fine
    file.WriteLine ""

  13. JODww says:

    the lines with a & before them

Skip to main content