Windows Scripting Host Programmer's ReferenceProgrammer's Reference
Windows Scripting Host Programmer's ReferenceProgrammer's Reference
PROGRAMMER'S REFERENCE
ABSTRACT
The following information is presented in this programmer's reference:
CONTENTS
INTRODUCTION
A Cscript Example
Document Conventions
Wscript Object
Wscript.Application
Page 1 of 44
Wscript.Arguments
Wscript.FullName
Wscript.Name
Wscript.Path
Wscript.ScriptFullName
Wscript.ScriptName
Wscript.Version
Wscript.CreateObject
Wscript.DisconnectObject
Wscript.Echo
Wscript.GetObject
Wscript.Quit
WshArguments Object
WshArguments.Item
WshArguments.Count
WshArguments.length
WshShell Object
WshShell.Environment
WshShell.SpecialFolders
WshShell.CreateShortcut
WshShell.ExpandEnvironmentStrings
WshShell.Popup
WshShell.RegDelete
WshShell.RegRead
Page 2 of 44
WshShell.RegWrite
WshShell.Run
WshNetwork Object
WshNetwork.ComputerName
WshNetwork.UserDomain
WshNetwork.UserName
WshNetwork.AddPrinterConnection
WshNetwork.EnumNetworkDrives
WshNetwork.EnumPrinterConnections
WshNetwork.MapNetworkDrive
WshNetwork.RemoveNetworkDrive
WshNetwork.RemovePrinterConnection
WshNetwork.SetDefaultPrinter
WshCollection Object
WshCollection.Item
WshCollection.Count
WshCollection.length
WshEnvironment Object
WshEnvironment.Item
WshEnvironment.Count
WshEnvironment.length
WshEnvironment.Remove
WshShortcut Object
WshShortcut.Arguments
Page 3 of 44
WshShortcut.Description
WshShortcut.FullName
WshShortcut.Hotkey
WshShortcut.IconLocation
WshShortcut.TargetPath
WshShortcut.WindowStyle
WshShortcut.WorkingDirectory
WshShortcut.Save
WshSpecialFolders Object
WshSpecialFolders.Item
WshSpecialFolders.Count
WshSpecialFolders.length
WshUrlShortcut Object
WshUrlShortcut.FullName
WshUrlShortcut.TargetPath
WshUrlShortcut.Save
INTRODUCTION
The Microsoft Windows operating system Scripting Host is a language-independent scripting host
for ActiveX scripting engines on 32-bit Windows platforms. Windows Scripting Host will be
integrated into Windows 98, Windows NT Workstation operating system version 5.0, and
Windows NT Server version 5.0.
Both Microsoft Visual Basic development system Scripting Edition (VBScript) and Microsoft
JScript development system scripting engines are provided with Windows Scripting Host. Other
software companies will provide ActiveX scripting engines for languages such as Perl, TCL, REXX,
Python, and others. Windows Scripting Host can be run from either the Windows-based host
(Wscript.exe), or the command-shell-based host (Cscript.exe).
Page 4 of 44
Microsoft Internet Explorer. Internet Explorer can execute scripts on client computers from within
HTML pages.
Internet Information Server (IIS). IIS supports Active Server Pages, so scripts can run on Web
servers. In other words, IIS supports server-side scripting over the Internet or an intranet.
Windows Scripting Host. Windows Scripting Host provides a low-memory scripting host so scripts
can execute directly on the Windows desktop or Command Prompt window; the scripts do not
need to be embedded in an HTML document. Windows Scripting Host is an ideal host for
noninteractive scripts that perform logon and administrative tasks.
In addition to having a smaller footprint than the other two scripting hosts, the Windows Scripting Host
does not rely on an HTML SCRIPT tag or LANGUAGE attribute to identify a script engine. Instead, it
uses the extension of the script file to determine what script engine to use. As a result, the scriptwriter
need not obtain a script engine ProgID. The scripting host itself maintains a mapping of script
extensions to ProgIDs and uses the Windows association model to launch the appropriate engine for
a given script.
For more information about how Windows Scripting Host works, see:
http://www.microsoft.com/management/wsh.htm.
OVERVIEW OF THE WINDOWS SCRIPTING HOST OBJECT MODEL
The Windows Scripting Host object model provides two main categories of ActiveX interfaces:
1. Script execution and troubleshooting: Properties and methods that are directly related to script
execution. This set of interfaces allows scripts to manipulate Windows Scripting Host, display
messages on the screen, and perform basic functions such as CreateObject and GetObject.
2. Helper functions: Properties and methods that map network drives, connect to printers,
retrieve and modify environment variables, and manipulate registry keys. These functions are
provided so administrators can use Windows Scripting Host to create simple logon scripts.
In addition to the object interfaces provided by Windows Scripting Host, administrators can use any
ActiveX controls that expose Automation interfaces to perform various tasks on the Windows platform.
For example, administrators can write scripts that use the Active Directory to manage the
Windows NT Directory Service.
USING WINDOWS SCRIPTING HOST
The following section describes how you can use Windows Scripting Host.
Revised January 1, 2009
Page 5 of 44
Page 6 of 44
Finds the CLSID from the scripting engine identifier; in this example, the CLSID is located in
HKCU\VBScript\CLSID.
The following table shows the registry entries that are necessary to register this engine (none of these
has a value name).
Key
Value
Name
Type
Value
.FOO
none
REG_SZ
FooFile
FooFile
none
REG_SZ
FooFile\ScriptEngine
none
REG_SZ
FooFile
FooFile\Shell\Open
none
REG_SZ
&Open
FooFile\Shell\Open\Comm
and
none
FooFile\Shell\Open2
none
REG_EXPA
Open &with command console
ND_SZ
FooFile\Shell\Open2\Com
mand
none
REG_SZ
%systemroot%\system32\wscript "%1"
%*
FooFile\ShellEx\
PropertySheetHandlers\
WSHProps
none
REG_SZ
{60254CA5-953B-11CF-8C9600AA00B8708C}
Page 7 of 44
Windows NT
Windows 98
Wscript
%SystemRoot%\System32\Wscript.exe
C:\Windows\Wscript.exe
Cscript
%SystemRoot%\System32\Cscript.exe
C:\Windows\Command\Cscript.exe
Double-clicking files or icons. These can be files or icons listed in My Computer, Windows
Explorer, the Find window, the Start menu, or on the Desktop.
Entering a script name at the Run command on the Start Menu. Press the Start button, select
Run, and enter the full name of the script you want to run, including file extension and any
necessary path information.
Entering Wscript.exe followed by a script name at the Run command. Press the Start button,
select Run, and enter Wscript, followed by the full name and path of the script you want to run.
If you double-click a script file whose extension has not yet been associated with Wscript.exe, an
Open With dialog box appears asking your what program you would like to use to open the file you
double-clicked. After choosing Wscript, if you check the Always use this program to open this file
check box, Wscript is registered as the default application for all files having the same extension as
the one you double-clicked. For example, if you check this check box when you run Chart.vbs,
Wscript.exe becomes the default application for all files having the .vbs extension.
Wscript.exe has a properties page associated with it that provides the following options:
Property
CSCRIPT
equivalent
Function
//T:nn
//logo or
//nologo
By using the Wscript.exe Properties page, you can set global scripting options for all scripts that
Revised January 1, 2009
Page 8 of 44
Page 9 of 44
Script name is the name of the script file, complete with extension and any necessary path
information, such as: d:\admin\vbscripts\chart.vbs.
Host options enable or disable various Windows Scripting Host features. Host options are
always preceeded by two slashes (//).
Script options are passed to the script. Script parameters are always preceeded by only one
slash (/).
Each parameter is optional; however, you cannot specify script options without specifying a script
name. If you do not specify parameters, Cscript displays the Cscript syntax and the valid host
parameters.
Cscript.exe supports the host parameters shown in the following table.\
Parameter
Description
//I
Interactive Mode: allows display of user prompts and script errors (this is
the default, and the opposite of //B).
//B
//T:nn
Enables time-out: the maximum number of seconds the script can run.
The default is no limit. (See the text following this table for more
information on this parameter.)
//logo
//nologo
//H:Cscript or
//H:Wscript
Page 10 of 44
//?
The //T parameter prevents excessive execution of scripts; it does this by setting a timer. When
execution time exceeds the specified value, Cscript interrupts the scripting engine using the
IActiveScript::InterruptThread method and terminates the process.
A Cscript Example
Several sample scripts are installed when you install the final release of the Windows Scripting Host.
These are also available for download at: http://www.microsoft.com/management/wsh.htm
Suppose, for the purposes of this example, that you have copied the Chart.vbs sample script to the
following folder on your computer:
c:\sample scripts\chart.vbs
You can run the script using with and without a logo, as follows:
1. Choose the MS-DOS operating system Command Prompt from Programs on the Start menu.
2. Enter the following commands at the command prompt (if your sample scripts are located in a
different folder, these lines should be modified accordingly):
cscript //logo c:\"sample scripts"\chart.vbs
cscript //nologo c:\"sample scripts"\chart.vbs
When Windows Scripting Host ships with Windows NT 5.0, you will no longer need to provide the
script file's extension. Instead, you can type the script name alone, or double-click the script in
Windows Explorer.
Document Conventions
Variable prefixes in parameter listings and code examples have the following meanings.
Prefix
Description
any
Any type
Boolean
int
Integer
Page 11 of 44
obj
str
String
Scripting examples are written in the Microsoft Visual Basic Scripting Edition (VBScript).
Methods written in Microsoft Jscript are case-sensitive. In general, you should use the same case as
the syntax examples shown in this document.
Parameters in brackets (for example, [anyValue]) are optional.
WINDOWS SCRIPTING HOST OBJECT REFERENCE
Objects provided by Wscript.exe
Wscript
WshArgument
Not exposed; accessed through the Wscript.Arguments property.
s
Automation object,
ProgID "Wscript.WshShell."
WshNetwork
WshShortcut
WshUrlShortcut
WshCollection
WshEnvironment
WshSpecialFolder
s
Page 12 of 44
N/A
Filename
Wscript.exe or Cscript.exe
CLSID
60254CA2-953b-11CF-8C96-00AA00B8708C
IID
60254CA1-953b-11CF-8C96-00AA00B8708C
The following table describes the properties associated with the Wscript object.
Property
Description
Application
Arguments
FullName
Name
Path
ScriptFullName Full path to the script being run by the Windows Scripting Host.
ScriptName
File name of the script being run by the Windows Scripting Host.
Version
The following table describes the methods associated with the Wscript object.
Method
Description
CreateObject
DisconnectOb
Disconnects a previously connected object from Windows Scripting Host.
ject
Echo
Page 13 of 44
Quit
Wscript.Application
The Application property provides the IDispatch interface on the Wscript object.
Syntax
Wscript.Application = objWscript
Wscript.Arguments
The Arguments property provides a WshArguments collection object.
Syntax
Wscript.Arguments = objArguments
Example
' Display all command-line parameters
Set objArgs = Wscript.Arguments
For I = 0 to objArgs.Count - 1
Wscript.Echo objArgs(I)
Next
Wscript.FullName
The FullName property provides a string containing the full path to the host executable.
Syntax
Wscript.FullName = strFullName
Example
Wscript.FullName = C:\WinNT\System32\wscript.exe
See Also
Wscript.Path property.
Wscript.Name
The Name property provides a string containing the friendly name of the Wscript object. This is the
default property.
Syntax
Revised January 1, 2009
Page 14 of 44
Page 15 of 44
Page 16 of 44
Page 17 of 44
Page 18 of 44
Page 19 of 44
N/A
Filename
Wscript.Exe or Cscript.Exe
CLSID
60254CA4-953b-11CF-8C96-00AA00B8708C
IID
60254CA3-953b-11CF-8C96-00AA00B8708C
The following table describes the properties associated with the WshArguments object.
Property
Description
Item
Count
length
WshArguments.Item
The Item property contains the natIndexth command-line parameter as a string. It is the default
property.
Syntax
Arguments(natIndex)
Arguments.Item(natIndex) = strArgument
Example Set oArgs = Wscript.Arguments.
Wscript.Echo oArgs(0)
Wscript.Echo oArgs.Item(0)
See Also
Wscript.Arguments property
WshArguments.Count The Count property provides the number of command-line parameters.
Syntax
Arguments.Count = natNumberOfArguments
See Also
Wscript.Arguments property, WshArguments.length property
Revised January 1, 2009
Page 20 of 44
Wscript.WshShell
Filename
WSHom.Ocx
CLSID
F935DC22-1CF0-11d0-ADB9-00C04FD58A0B
IID
F935DC21-1CF0-11d0-ADB9-00C04FD58A0B
The following table describes the properties associated with the WshShell object.
Proper
Description
ty
Enviro
nment
Specia
Provides access to Windows shell folders such as the desktop folder, start menu
lFolder
folder, and personal document folder, using the WshSpecialFolders object.
s
The following table describes the methods associated with the WshShell object.
Method
Description
CreateShortcut
ExpandEnvironmen
Expands a PROCESS environment variable and returns the result string.
tStrings
Revised January 1, 2009
Page 21 of 44
RegDelete
RegRead
RegWrite
Run
WshShell.Environment
The Environment property returns the WshEnvironment object.
Syntax
WshShell.Environment ( [strType]) = objWshEnvironment
Remarks
If strType specifies where the environment variable resides, possible values are "System," "User,"
"Volatile," and "Process." If strType is not supplied, this method retrieves the system environment
variables in Windows NT or the process environment variables in Windows 95. For Windows 95, only
"Process" is supported in the strType parameter. The following variables are provided with the
Windows operating system. Scripts can also get environment variables that were set by other
applications.
Name
Description
NUMBER_OF_PROCESSORS
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
OS
COMSPEC
Page 22 of 44
HOMEPATH
PATH
PATHEXT
PROMPT
SYSTEMDRIVE
SYSTEMROOT
WINDIR
TEMP
TMP
Example
' Retrieve the NUMBER_OF_PROCESSORS system environment variable
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
See Also
WshEnvironment object
WshShell.SpecialFolders
The SpecialFolders property provides the WshSpecialFolders object for accessing Windows shell
folders such as the desktop folder, start menu folder, and personal document folder.
Syntax
WshShell.SpecialFolders = objWshSpecialFolders
Revised January 1, 2009
Page 23 of 44
Page 24 of 44
Icon Types
Value Description
16
32
48
64
The preceding two tables do not cover all values for natType. For a complete list, see the Win32
Revised January 1, 2009
Page 25 of 44
[OK] button
[Cancel] button
[Abort] button
[Retry] button
[Ignore] button
[Yes] button
[No] button
Example
Set WshShell = Wscript.CreateObject("Wscript.Shell") WshShell.Popup "Where do you want to go
today?"
See Also
Wscript.Echo method
WshShell.RegDelete
The RegDelete method deletes from the registry the key or value named strName.
Syntax
WshShell.RegDelete strName
Parameters
strName
If strName ends with a backslash character (\), this method deletes the key instead of the value. The
strName parameter must begin with one of following root key names:
Short
Long
Page 26 of 44
Long
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIG
Page 27 of 44
Long
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIG
Remarks
RegWrite supports strType as REG_SZ, REG_EXPAND_SZ, REG_DWORD and REG_BINARY. If
another data type is passed as strType, RegWrite returns E_INVALIDARG.
Revised January 1, 2009
Page 28 of 44
Page 29 of 44
Wscript.WshNetwork
Filename
WSHom.Ocx
CLSID
F935DC26-1CF0-11d0-ADB9-00C04FD58A0B
IID
F935DC25-1CF0-11d0-ADB9-00C04FD58A0B
The following table describes the properties associated with the WshNetwork object.
Property
Description
ComputerName
UserDomain
UserName
The following table describes the methods associated with the WshNetwork object.
Method
Description
AddPrinterConnection
EnumNetworkDrives
EnumPrinterConnections
MapNetworkDrive
RemoveNetworkDrive
RemovePrinterConnection
SetDefaultPrinter
WshNetwork.ComputerName
The ComputerName property provides a string representation of the computer name.
Page 30 of 44
Page 31 of 44
Page 32 of 44
Page 33 of 44
Page 34 of 44
N/A
Filename
WSHom.Ocx
CLSID
F935DC24-1CF0-11d0-ADB9-00C04FD58A0B
IID
F935DC23-1CF0-11d0-ADB9-00C04FD58A0B
The following table describes the properties associated with the WshCollection object.
Property
Description
Item
Count
length
WshCollection.Item
The Item property provides the natIndexth enumerated item as a string. It is the default property.
Syntax
WshCollection(natIndex) = strEnumeratedItem
WshCollection.Item(natIndex) = strEnumeratedItem
Example
Set WshNetwork = Wscript.CreateObject("Wscript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Wscript.Echo oDrives.Item(0) = " "Z:"
Wscript.Echo oDrives.Item(1) = "\\Server\Share"
WshCollection.Count
The Count property provides the number of enumerated items.
Syntax
WshCollection.Count = natNumberOfItemsl
Revised January 1, 2009
Page 35 of 44
N/A
Filename
WSHom.Ocx
CLSID
IID
The following table describes the properties associated with the WshEnvironment object.
Property
Description
Item
Count
length
The following table describes the method associated with the WshEnvironment object.
Method Description
Page 36 of 44
WshEnvironment.Item
The Item property sets or returns the value for the strName environment variable. It is the default
property.
Syntax
WshEnvironment.Item("strName") = strValue
WshEnvironment("strName") = strValue
Example
' Get the value of NUMBER_OF_PROCESSORS environment variable
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
' Set the value of EXAMPLE volatile environment variable to A_VALUE
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshEnv = WshShell.Environment("VOLATILE")
WshEnv("EXAMPLE")= "A_VALUE"
' List all system environment variables
Set WshShell = Wscript.CreateObject("Wscript.Shell")
For Each strVarName In WshShell.Environment("SYSTEM")
MsgBox strVarName
Next
See Also
WshShell.Environment property
WshEnvironment.Count
The Count property provides the number of enumerated items.
Syntax
WshEnvironment.Count = natNumberOfItems
See Also
WshEnvironment.length property
Revised January 1, 2009
Page 37 of 44
N/A
Filename
WSHom.Ocx
CLSID
F935DC28-1CF0-11d0-ADB9-00C04FD58A0B
IID
F935DC27-1CF0-11d0-ADB9-00C04FD58A0B
Page 38 of 44
Description
Arguments
Description
Hotkey
IconLocation
TargetPath
WindowStyle
WorkingDirectory
The following table describes the method associated with the WshShortcut object.
Method Description
Save
WshShortcut.Arguments
The Arguments property provides parameters to a shortcut object.
Syntax
WshShortcut.Arguments = strArguments
WshShortcut.Description
The Description property provides a description of a shortcut object.
Syntax
WshShortcut.Description = strDescription
WshShortcut.FullName
The FullName property provides the full path of a shortcut object.
Syntax
WshShortcut.FullName = strFullName
Revised January 1, 2009
Page 39 of 44
Page 40 of 44
N/A
Filename
WSHom.Ocx
CLSID
IID
The following table describes the properties associated with the WshSpecialFolders object.
Property
Description
Item
Page 41 of 44
length
WshSpecialFolders.Item
The Item property returns the full path for the special folder specified by strFolderName. It is the
default property.
Syntax
WshShell.SpecialFolders.Item("strFolderName") = strFolderPath
WshShell.SpecialFolders("strFolderName") = strFolderPath
Remarks
WshShell.SpecialFolders("strFolderName") returns NULL if the requested folder (strFolderName) is
not available. For example, Windows 95 does not have an AllUsersDesktop folder and returns NULL if
strFolderName = AllUsersDesktop
The following special folders are provided with the Windows 95 and Windows NT 4.0 operating
systems:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
StartupB
Templates
Example
' This fragment returns the full path for the Windows Desktop folder
Set WshShell = Wscript.CreateObject("Wscript.Shell")
StrMyDesktop = WshShell.SpecialFolders("Desktop")
Revised January 1, 2009
Page 42 of 44
N/A
Filename
WSHom.Ocx
CLSID
IID
The following table describes the properties associated with the WshUrlShortcut object.
Revised January 1, 2009
Page 43 of 44
Description
FullName
TargetPath
The following table describes the method associated with the WshUrlShortcut object.
Property
Description
Save
WshUrlShortcut.FullName
The FullName property provides the full path of a shortcut object.
Syntax
WshUrlShortcut.FullName = strFullName
WshUrlShortcut.TargetPath
The TargetPath property provides the target path of a shortcut object.
Syntax
WshUrlShortcut.TargetPath = strTargetPath
WshUrlShortcut.Save
The Save method saves a shortcut to the location specified by the FullName property.
Syntax
WshUrlShortcut.Save
FOR MORE INFORMATION
For the latest information on Windows NT Server, check out our World Wide Web site at
http://www.microsoft.com/ntserver or the Windows NT Server Forum on the Microsoft Network (GO
WORD: MSNTS).
Page 44 of 44