Run an external Command.
Syntax objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn]) Key objShell : A WScript.Shell object strCommand : The Command to be executed intWindowStyle (Optional) : Int value indicating the appearance of the program's window. Not all programs make use of this. bWaitOnReturn : Wait for the command to complete before continuing execution of the wsh script.
If bWaitOnReturn is set to TRUE, the Run method returns
any error code returned by the application.
If bWaitOnReturn is not specified or FALSE, this method immediately returns to script execution rather than waiting on the process termination (and returns an error code of 0)
Specifying the bWaitOnReturn parameter allows you to run programs synchronously (one at a time).
Environment variables within the argument strCommand are automatically expanded.
If a file type has been properly registered to a particular program, calling run on a file of that type executes the program. For example, if Word is installed on your computer system, calling Run on a *.doc file starts Word, and loads the document.
Settings for intWindowStyle:
0 Hide the window (and activate another window.)
1 Activate and display the window. (restore size and position) Specify this flag when displaying a window for the first time.
2 Activate & minimize.
3 Activate & maximize.
4 Restore. The active window remains active.
5 Activate & Restore.
6 Minimize & activate the next top-level window in the Z order.
7 Minimize. The active window remains active.
8 Display the window in its current state. The active window remains active.
9 Restore & Activate. Specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.
This one line VBScript can be used to run a command in an invisible window:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
' An example running 'Demo.cmd' with invisible.vbs
wscript.exe "invisible.vbs" "demo.cmd" //nologo
Launch Notepad with the current executed script:
Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run ("%windir%\notepad.exe " & WScript.ScriptFullName)
Launch Notepad with the current executed script, specify the window type, wait for Notepad to be shut down by the user, and save the error code returned from Notepad when it exits:
Set objShell = WScript.CreateObject("WScript.Shell") intReturn = objShell.Run("notepad.exe " & WScript.ScriptFullName, 1, true) If intReturn <> 0 Then Wscript.Echo "Error running program" End If
Open a cmd window, change the path to C:\ , and execute the DIR command:
Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "cmd /K CD C:\ & Dir" Set objShell = Nothing
Call one VB script from another. This can be done as shown below, although it is usually better to put everything in a single script and use Functions to split up the blocks of code.
Set objShell = CreateObject("WScript.Shell")
“Tell the truth and run” ~ Yugoslavian proverb
.Exec - Execute command, returning an object
.ShellExecute - Run an application in the Windows Shell
cscript - Run a VB Script .vbs file
Arguments - Passing arguments to a script
Equivalent Windows CMD command: START - Start a program or command
Equivalent PowerShell cmdlet: Run