.Run

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.

Invisible.vbs

In the Windows Task Scheduler the task properties include a tick box for "Hidden", this does not minify or hide the command window, but does hide the task from Task Scheduler’s list of tasks. (The menu View ➞ Show hidden tasks is by default ticked.)

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

If running this as a scheduled task, either provide the full path to the VBS and Batch file, or set the 'Start In' folder when creating the task.

If you also need to pass parameters to the batch file, then use this version of invisible.vbs which passes them along (source Pyprohly):

Dim Args()
ReDim Args(WScript.Arguments.Count - 1)

For i = 0 To WScript.Arguments.Count - 1
   Args(i) = """" & WScript.Arguments(i) & """"
Next

CreateObject("WScript.Shell").Run Join(Args), 0, False

Minimized.vbs

This one line VBScript can be used to run a command in a minimized window:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 7, False

An example running 'Demo.cmd' with minimized.vbs
%SystemRoot%\system32\wscript.exe "minimized.vbs" "demo.cmd" //nologo

Examples

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")
objShell.run("cscript C:\scripts\demo.vbs")

“Tell the truth and run” ~ Yugoslavian proverb

Related:

.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


 
Copyright © 1999-2024 SS64.com
Some rights reserved