Click here to Skip to main content
15,942,847 members
Articles / Programming Languages / VBScript

Differences between Run and Exec (VBScript)

Rate me:
Please Sign up or sign in to vote.
3.52/5 (7 votes)
12 Mar 2013CPOL2 min read 349.3K   3   11   8
Learn to call external applications in VBScript.


When I started programming in VBScript, I didn't know the real difference between Run and Exec in VBScript present in the WScript.Shell object. I'm writing this tip to help you to know when to use Run or Exec and I will show examples that can be implemented in your code (async command-line reading is an interesting thing!).   


When you need to run an external program that does not have COM access, you need to call CMD. This tip pretends to show you the right choice depending on your objective and program you are calling.  


We use Run when we are calling an external command-line program (or any command that the prompt recognizes). It can return %ERRORLEVEL% and with this code, we can show a message to the user saying if the operation was success or not. It doesn't support async output, it means that you can't show to the user the output of the external program in real-time, you need to wait till the program exits. It does not support it because Run() reads the exit code that is released only when an external application exits.

Set objShell = CreateObject("WScript.Shell")
strErrorCode = objShell.Run ipconfig,0,True
WScript.Echo strErrorCode

With this, ipconfig will be executed in command prompt. The CMD prompt will not be shown (0) and our script will wait till the program exits before it continues processing (True).  

ERRORLEVEL will be set in the strErrorCode variable. 


We can use it to call GUI applications, because then Run does not call CMD to execute the action.

The principal difference between Exec and Run is that Exec supports real-time output to the user (if it is a command-line program), but we can't get ERRORLEVEL using this method.  

 If we want to call explorer.exe (or an external program), we just need to call it.

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("explorer.exe")

For async output, we need to call CMD.exe and read the output with VBScript.

Note: Some programs do not support real-time output (WGet and DISM are examples). Use Run instead.

Set objShell = CreateObject("WScript.Shell")
comspec = objShell.ExpandEnvironmentStrings("%comspec%")
' ////////////////////////////////////////////////////////

Set objExec = objShell.Exec(comspec & " /c ipconfig")
     LoopWhile Not objExec.Stdout.atEndOfStream

Image 1

We expand the %comspec% variable (it returns the location of the command-line interpreter of Windows). If you're running Windows NT, it returns CMD.exe. For Windows 9x users, will be called. It will call the correct version of CMD.exe depending on the ARCH of your OS. For example: if you have Windows x64, C:\Windows\system32\cmd.exe (64-bits CMD) will be called, not C:\Windows\SysWow64\cmd.exe (32-bit version). It is important for DISM, for example, that it fails if we try to modify an image if 32-bit CMD is called on an x64-bit environment. 

After calling the command-line, it will read the output in real-time and write changes on the same window of our script. When the program closes, VBScript will read all output from the prompt and write it on screen. 


  • 12 Dec 2012: Tip published.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

PraiseCode formatting tip Pin
Konstantin Pandipulovich17-Nov-19 23:13
Konstantin Pandipulovich17-Nov-19 23:13 
Questiongracias!!! Pin
Member 1122442126-Jul-17 3:04
Member 1122442126-Jul-17 3:04 
Questionre: Pin
Member 1141270229-Jan-15 8:07
Member 1141270229-Jan-15 8:07 
AnswerRe: re: Pin
Eduardo Mozart de Oliveira16-Oct-16 4:42
Eduardo Mozart de Oliveira16-Oct-16 4:42 
I fixed it. But I think that CodeProject formatting when saving replaces the Quotes.
You can try to type the code manually into Notepad (it is less than 3 lines...)
And if you're still using Windows XP into 2015 I recommend upgrading it to a newer version of Windows or even Linux. You can use ReactOS if your PC can't be upgraded to a newer version of Windows - it is the best of the two worlds.
Best regards,
Eduardo Mozart de Oliveira.
QuestionNeed assistance Pin
Member 110557503-Sep-14 1:52
Member 110557503-Sep-14 1:52 
AnswerRe: Need assistance Pin
Eduardo Mozart de Oliveira16-Oct-16 4:38
Eduardo Mozart de Oliveira16-Oct-16 4:38 
QuestionThank you Pin
Kevin Dondrea16-Jan-14 2:44
Kevin Dondrea16-Jan-14 2:44 
AnswerRe: Thank you Pin
Eduardo Mozart de Oliveira27-Jan-14 9:57
Eduardo Mozart de Oliveira27-Jan-14 9:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.