.Shell - VBScript
Overview
The .Shell
method lets VBScript seamlessly execute programs and scripts directly from within your scripts. This powerful method provides a convenient way to leverage external tools, launch applications, and perform system-level tasks in your scripts.
Syntax
object.Shell(pathname, style, waitOnReturn)
Parameters:
- pathname: The target program or script to execute.
- style: An optional parameter that specifies how the target program window should appear. Possible values are:
- 0 (vbHide): Hides the window.
- 1 (vbNormalFocus): Activates and focuses the window.
- 2 (vbMinimizedFocus): Minimizes the window and gives it focus.
- 3 (vbMaximizedFocus): Maximizes the window and gives it focus.
- 4 (vbNormalNoFocus): Activates the window but doesn’t give it focus.
- 5 (vbMinimizedNoFocus): Minimizes the window but doesn’t give it focus.
- 6 (vbMaximizedNoFocus): Maximizes the window but doesn’t give it focus.
- waitOnReturn: An optional parameter that specifies whether VBScript should wait for the target program to finish executing before continuing. Possible values are:
- True: Waits for the target program to finish.
- False (default): Doesn’t wait for the target program to finish.
Options/Flags
None
Examples
Simple Example
WScript.Shell "notepad.exe"
This launches Notepad and displays it normally.
Using Style Option
WScript.Shell "calc.exe", vbMaximizedFocus
This opens the Calculator in a maximized window and gives it focus.
Wait Until Completion
WScript.Shell "cmd.exe /c dir c:", True
This executes a directory listing in the command window and waits for its completion before continuing.
Common Issues
- Target Program Not Found: Ensure the specified pathname is accurate and the target program/script is available in the system path.
- Permission Denied: Confirm that the user running the script has sufficient permissions to execute the target program.
Integration
The .Shell
method can be combined with other commands in VBScript to create complex automations. For instance, you can use .Stdout
to capture the output of a command executed using .Shell
.
Related Commands
CreateObject
: Allows scripts to create ActiveX objects.FileSystemObject
: Manipulate the file system.