Jul 2 2008

How to redirect output when using wscript.shell in vbscript

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

Google Query: Wscript.Shell Application redirection output

I was trying to run ipconfig in one of my vbscripts using wscript.shell and the run method. However, I was trying to redirect the output to a text file. It just wasn’t working. It was a combination of those tricky double quotes and me not knowing the proper syntax to accomplish this from inside a vbscript. Thankfully I found the answer quickly after some googling.

There was a post at the ScriptingAnswers.com forum that had a solution to my problem. Basically, it can be summed up with the following code:

Set objShell = WScript.CreateObject("WScript.Shell")
strRun = "%comspec% /c ipconfig.exe > " & AddQuotes("C:\temp\ipconfig.txt")
objShell.Run strRun, 1, True
Function AddQuotes(strInput)
AddQuotes = Chr(34) & strInput & Chr(34)
End Function

The function AddQuotes takes care of the double quoting issues by surrounding whatever is input (using the built in strInput argument of the AddQuotes function.) into the AddQuotes function at the beginning of the script with additional quotes using the built in Chr(34) function.

As for the %compspec% /c syntax. That is covered in this Microsoft KB Article.

That’s it! You should now be able to redirect the output of any shell command using this technique. Happy Scripting!

Technorati Tags: , , ,

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

4 Comments on this post


  1. Rodney said:

    strInput is just the argument for the AddQuotes function. It’s not a built in function.

    August 21st, 2008 at 9:34 am
  2. hstagner said:

    Hello Rodney,

    Thank you for the correction. I have edited the post accordingly. Thank you for reading!

    August 21st, 2008 at 10:28 am
  3. Bal said:


    In this how can I pass command line arguments to the bat (or exe) along with logging

    March 17th, 2010 at 1:02 pm
  4. Som.DT said:

    Set objShell = WScript.CreateObject(”WScript.Shell”)
    objShell.Run “%comspec% /c ipconfig.exe > “”C:\temp\ipconfig.txt”"”, 1, True

    July 13th, 2010 at 8:22 am


Subscribe Form

Subscribe to Blog


Recent Readers

                  Computers Blogs - Blog Top Sites