Windows Script Host Version 2.0 Tutorial

To | See
See a list of conventions used throughout this tutorial | Document Conventions
Get an overview of Windows Script Host | About Windows Script Host
Learn about Windows Script (.wsf) files | Using Windows Script Files
Customize the way your scripts run | Customizing Script Settings
Create files for setting properties in your scripts | Setting Script Properties
Get an overview of running scripts | Running Your Scripts
Run scripts from the Windows environment | Running Scripts with WScript.exe
Run scripts from the command prompt | Running Scripts with CScript.exe
Run scripts with drag and drop functionality | Drag and Drop Support
View the Windows Script Host Reference | Windows Script Host Reference
View a list of Windows Script Host error messages | Windows Script Host Error Messages
View a list of features by version | Version Information

©2000 Microsoft Corporation. All rights reserved.


Document Conventions

Microsoft Windows Script Host 2.0 Language Reference

Document Conventions The following conventions are used throughout this tutorial:

  • Scripting examples are written in the Microsoft Visual Basic® Scripting Edition (VBScript) language.
  • Methods written in Microsoft JScript® are case-sensitive. In general, you should use the same case as the syntax examples in this document.
  • Parameters in brackets (for example, [anyValue]) are optional.

The following table lists variable prefixes in parameter listings and code examples, and their corresponding descriptions.

Prefix | Description
any | Any type
b | Boolean
int | Integer
nat | Natural number or nonnegative integer
obj | Object (IDispatch interface)
str | String


About Windows Script Host

Microsoft® Windows® Script Host is a language-independent scripting host for ActiveX® scripting engines. It brings simple, powerful, and flexible scripting to the Windows 32-bit platform, allowing you to run scripts from both the Windows desktop and the command prompt.

Windows Script Host is ideal for non-interactive scripting needs such as logon scripting, administrative scripting, and machine automation.

The Benefits of Windows Script Host

Windows Script Host offers the following benefits:

  • Two separate executable files, WScript.exe and CScript.exe, for running scripts either from Windows or from the command prompt. WScript.exe provides a Windows-based properties page for setting script properties, while CScript.exe provides command-line switches for setting script properties.
  • Support for multiple files. You can call multiple scripting engines and perform multiple jobs from a single Windows Script (.wsf) file.
  • Very low memory requirements.
  • Mapping of script extensions to programmatic identifiers (ProgIDs). When you start a script from the Windows desktop or the command prompt, the script host reads and passes the specified script file contents to the registered script engine. Instead of using the HTML SCRIPT tag to identify the script, the host uses file extensions; for example, .vbs for Microsoft Visual Basic® Scripting Edition (VBScript) files, and .js for Microsoft JScript® files. The use of extensions means you no longer need to be familiar with the ProgID for a given script engine. Windows Script Host handles this for you by maintaining a mapping of script extensions to ProgIDs, launching the appropriate engine for a given script.

Windows Script Host Object Model

Windows Script Host provides ActiveX interfaces for direct manipulation of script execution, as well as helper functions for other actions. The Windows Script Host Referencedocuments elements, methods, objects, and properties with which you can accomplish tasks such as the following:

  • Print messages to the screen
  • Run basic functions such as CreateObject and GetObject
  • Map network drives
  • Connect to printers
  • Retrieve and modify environment variables
  • Modify registry keys

Windows Script Host Integration

Windows Script Host is integrated into Windows 98, Windows 2000 Professional, and Windows 2000 Server. Version 2.0 includes the VBScript and JScript scripting engines. In the future, other software companies may provide ActiveX scripting engines for languages such as PerlScript, TCL, REXX, and Python.


Using Windows Script Files

The Windows script (.wsf) file, a text document containing Extensible Markup Language (XML) code, incorporates several features that offer you increased scripting flexibility. Because Windows script files are not engine-specific, they can contain script from any ActiveX®-compliant scripting engine.

With .wsf files, you can take advantage of the following benefits as you create your scripts:

  • Support for Include statements allows you to incorporate previously-written functions from VBScript or JScript files into your Windows Script Host project.
  • Support for multiple engines allows you to use more than one scripting language per file.
  • Support for Type libraries allows you to add constants to your code.
  • Support for tools allows you to edit files with any XML editor.
  • Support for multiple jobs in one file allows you to store all of your code in a single location.

Include Statements

If you have .js and .vbs files from previous Windows Script Host projects, a .wsf file enables you to use them with Windows Script Host. A .wsf file encapsulates a library of functions that can in turn be used by multiple .wsf files.

The following example shows the contents of a .wsf file that includes a JScript file (fso.js), plus a VBScript function that calls a function (GetFreeSpace) in the included file. The contents of fso.js are provided also.

<Job id="IncludeExample">
<script language="JScript" src="FSO.JS"/>
<script language="VBScript">
   ' Get the free space for drive C.
   s = GetFreeSpace("c:")
   WScript.Echo s
</Script>
</Job>

Contents of fso.js:

function GetFreeSpace(drvPath) {
   var fs, d, s;
   fs = new ActiveXObject("Scripting.FileSystemObject");
   d = fs.GetDrive(fs.GetDriveName(drvPath));
   s = "Drive " + drvPath + " - " ;
   s += d.VolumeName;
   s += " Free Space: " + d.FreeSpace/1024 + " Kbytes";
   return s;
}

Multiple-Engine Support

Since one scripting language may not have all the functionality you need, Windows Script Host allows you to combine multiple languages in a single .wsf file. The following example shows the contents of a .wsf file that includes both VBScript® and PerlScript code:

<job id="PERLandVBS">
<script language=PerlScript RUNAT=Server>
   sub PerlHello
   {
      my $str = @_[0];
      $WScript->Echo($str);
   }
</script>

<script language="VBScript">
   WScript.Echo "Hello from VBScript"
   PerlHello "Hello from PERLScript"
</Script>
</Job>

Type Library Support

In the following example, MyComponent was developed with Microsoft® Visual Basic® 5.0. MyComponent defines the constant MyError with the following statement.

Public Const MyError = "You are not using MyComponent correctly."

The type library is contained in mycomponent.lib, which you have installed in C:\MyComponent.

<Job id="IncludeExample">
<Reference progid="MyComponent.MyClass">
<Script language="VBScript">
   Dim MyVar
   Set MyVar = CreateObject("MyComponent.MyClass")
   Currentreturn = MyVar.MyMethod
   If Currentreturn = False then
      WScript.Echo MyError
   End If
</Script>
</Job>

Tools Support

Since the .wsf file is in XML format, you can use any editor that supports XML to edit .wsf files. You can also use any text editor, such as Notepad.

Multiple Jobs in One File

Instead of keeping all your scripts in separate files, you can incorporate them all into one .wsf file and break them into several different jobs. You can then run each job separately using syntax similar to the following example, where MyFirstJob is the name of the job contained in the MyScripts.wsf file:

CScript //Job:MyFirstJob MyScripts.wsf


Customizing Script Settings

You can record specific settings for each of your individual scripts by means of a Windows Script Host control (.wsf) file. The .wsf file is a text document in which you can customize execution of one or more of your scripts.

You can create multiple .wsf files for a single script, which enables you to tailor the way the script runs to the needs of specific groups or even individuals within an organization. Thus a single logon script, invoked by two different .wsf files containing different settings and parameters, can be made to serve two very different groups.

The .wsf file is analogous to the PIF file used to run older 16-bit applications. It is created automatically when you set the properties for a supported script file. You can treat it as if it were an executable file or batch file.

For example, suppose you have a script named Myscript.vbs for which you have created a .wsf file named Myscript.wsf. To run Myscript.vbs with the options recorded in Myscript.wsf, you can either double-click Myscript.wsf in Windows Explorer, or pass Myscript.wsf as a script argument to CScript.exe or WScript.exe. You could do the latter by entering the following command at the command prompt:

c:\>cscript Myscript.wsf

For details, see Setting Script Properties.


Setting Script Properties

For each individual script you run, you can record specific settings by means of a control (.wsf) file. A .wsf file is a text file that uses a format similar to that of .ini files. It is created automatically when you set the properties for a supported script file.

To create a .wsf file for a given script

  1. Right-click the script file in Windows Explorer.
  2. Choose Properties on the context menu.
  3. Choose the settings you want for the script on the property page.
  4. Click OK or Apply.

    A .wsf file is created with the same name as the script file you right-clicked. A sample .wsf file might contain the following text:

    [ScriptFile]
Path=C:\WINNT\Samples\WSH\showprop.vbs
[Options]
Timeout=0
DisplayLogo=1
BatchMode=0

    The Path setting in the [ScriptFile] section identifies the script file that this .wsf file is associated with. The keys in the [Options] section correspond to settings in the Script tab within the Properties dialog box.

How the .wsf file works

When you double-click the .wsf file or execute it from the command line, CScript.exe or WScript.exe reads the .wsf file to determine the specific settings that should be used to execute the script. CScript/WScript executes the original script, passing in the properties that are defined within the .wsf file.

It is important to note that you must have the original script file present when executing the .wsf file. If the .wsf file fails to execute the script, check the Path= entry in the .wsf file to ensure that it is pointing to the script you are attempting to run.


Running Your Scripts

To | See
Run a script from the Windows environment | Running Scripts with WScript.exe
Run a script from the command prompt | Running Scripts with CScript.exe
Use drag and drop functionality | Drag and Drop Support


Running Scripts with WScript.exe

WScript.exe is the version of Windows Script Host that enables you to run scripts from Windows. WScript.exe provides a Windows-based dialog box for setting script properties. Using WScript.exe, you can run scripts under Windows in the following ways:

  • Double-click 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.
  • Enter a script name at the Run command on the Start menu. Press the Start button, choose Run, and then enter the full name of the script you want to run, including its file extension and any necessary path information.
  • Enter 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 which program to use to open the file. Choose WScript and check Always use this program to open this file check box to register WScript 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.

The WScript.exe properties dialog box provides the following options:

Property | Description | CSCRIPT equivalent
Stop script after specified number of seconds. | Specifies the maximum number of seconds that a script can run. The default is no limit. | //T:nn
Display logo when script is executed in command console. | Displays a banner before running the script. This is the default. The opposite is //nologo. | //logo or //nologo

Using the WScript.exe Properties dialog box, you can set global scripting options for all scripts that WScript runs on the local machine. You can also set options for individual scripts, using a .wsf file. For details, see Setting Script Properties.

When Windows Script Host ships with Windows NT® 2000, 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.


Running Scripts with CScript.exe

CScript.exe is the version of Windows Script Host that enables you to run scripts from the command prompt. CScript.exe provides command-line switches for setting script properties. To use CScript.exe, type a command line at the command prompt using the following syntax:

cscript [host options...] [script name] [script options and parameters]

where the terms are defined as follows:

  • Host options enable or disable various Windows Script Host features. Host options are preceded by two slashes (//).
  • Script name is the name of the script file, complete with extension and any necessary path information, such as: d:\admin\vbscripts\chart.vbs.
  • Script options and parameters are passed to the script. Script parameters are preceded by a single 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 options 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 | Batch mode: suppresses command-line display of user prompts and script errors.
//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 | Default. Displays a banner (opposite of //nologo).
//nologo | Prevents display of an execution banner at run time.
//H:CScript or //H:WScript | Registers CScript.exe or WScript.exe as the default application for running scripts. If neither is specified, WScript.exe is assumed as the default.
//S | Saves the current command-line options for this user.
//? | Shows command usage (same as with parameters).
//E:engine | Executes the script with the specified scripting engine.
//D | Turns on the debugger.
//X | Launches the program in the debugger.
//Job:<JobID> | Runs the specified JobID from the .wsf file.

The //T parameter prevents excessive execution of scripts 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 Script Host. These are also available for download at: http://msdn.microsoft.com/scripting.

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 with and without a logo, as follows:

Choose the MS-DOS® operating system command prompt from Programs on the Start menu.

Enter the following commands at the command prompt (modify accordingly if your sample scripts are located in a different folder):

cscript //logo c:\"sample scripts"\chart.vbs
cscript //nologo c:\"sample scripts"\chart.vbs


Drag and Drop Support

The Windows operating system currently supports drag and drop with batch files. This functionality, which enables you to drag one or more files onto a batch file and then run it with the files as arguments, is now supported in Windows Script Host version 2.0 as well.

When you drag files onto a WSH script, the file names are translated into arguments on the command line. These file names can be displayed in a list, which you can use to manipulate files with any scripting object.

To display a script's argument list

  1. Create a file and give it a name with a .vbs extension (for example, DragDrop.vbs).
  2. Add the following code to the .vbs file:
    Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   WScript.Echo objArgs(I)
Next
  3. Save the file to your hard drive.
  4. Drag and drop any file or files onto your .vbs file. The file names are echoed back to the screen.

Remarks

The number of files you can drag onto a script is limited by the maximum command-line length your system allows. If the total number of characters in all file names being dragged exceeds this limit, the dragging and dropping operation will fail.


<?job ?> Element

Microsoft Windows Script Host 2.0 Language Reference

<?job?> Element

XML processing instruction that specifies attributes for error handling.

<?job error="flag" debug="flag" ?>

Arguments

error, debug

A Boolean value: "true" or "false," 1 or 0, or "yes" or "no." "False" is the default value for all attributes.

Remarks

Although most batch files normally run silently during production, you might find it useful to be notified of errors in the Windows Script Component file as you are developing it. You can specify the following types of error notifications:

Attribute | Description
error | Set to "true" to allow error messages for syntax or run-time errors in the Windows Script file.
debug | Set to "true" to enable debugging. If debugging is not enabled, you will be unable to launch the script debugger for a WSH batch file.

Example

The following example incorporates two jobs into one .wsf file using two different scripting languages:

<package>

<job id="DoneInVBS">
<?job debug="true"?>
<script language="VBScript">
   WScript.Echo "This is VBScript"
</script>
</job>

<job id="DoneInJS">
<?job debug="true"?>
<script language="JScript">
   WScript.Echo("This is JScript");
</script>
</job>

</package>


<?XML ?> Element

Microsoft Windows Script Host 2.0 Language Reference

<?XML?> Element

Indicates that a file should be parsed as XML.

<?XML version="version" [standalone="DTDflag"] ?>

Arguments

version

A string in the form n.n specifying the XML level of the file. Use the value 1.0.

DTDflag

Optional. A Boolean value indicating whether the XML file includes a reference to an external Document Type Definition (DTD). Script component XML files do not include such a reference, so the value for this attribute is always "yes."

Remarks

This declaration must be the first element in the file, and cannot be preceded by any blank lines.

The existence of this declaration puts the script component compiler into strict XML mode, where element types and attribute names are case-sensitive, attribute values are enclosed in single or double quotation marks, and all elements are parsed. If the declaration is not included, the compiler allows syntax that is less strict.

You should include this declaration and follow XML conventions if your script component file will be edited in an editor that supports XML.

Example

<?XML version="1.0" standalone="yes" ?>


<job> Element

Allows multiple jobs to be defined in a single file. Because this is an enclosing tag, every tag within the Job tag will apply to that job.

<job [id=JobID]>
   job code
</job>

Arguments

JobID

Optional. The unique (to the file) identifier of the job.

Example

The following example incorporates two jobs into one .wsf file using two different scripting languages:

<package>

<job id="DoneInVBS">
<?job debug="true"?>
<script language="VBScript">
   WScript.Echo "This is VBScript"
</script>
</job>

<job id="DoneInJS">
<?job debug="true"?>
<script language="JScript">
   WScript.Echo("This is JScript");
</script>
</job>

</package>


<object> Element

XML element used in Windows Script component files that defines objects that can be referenced by script.

<object id="objID" [classid="clsid:GUID" | progid="progID"] />

Arguments

objID

A name by which the object can be referenced in your script. Object ID values must begin with a letter and can include letters, digits, and underscores (_). The object ID must be unique throughout the scope of the Windows Script Component. For example, if you specify the name CObj, you can reference the object in your script as follows:

x = CObj.Prop1

GUID

(Optional) A reference to the class ID (GUID) under which the object has been registered. Use "clsid:" followed by the class ID (without braces). You must specify either a classid or a progid attribute, as in the following example:

classid="clsid:2154c700-9253-11d1-a3ac-0aa0044eb5f"

progID

(Optional) The program ID of the object, which can be specified as an alternative to the class ID. You must specify either a classid or a progid attribute.

Remarks

The <object> element provides a way to expose objects globally for use in scripting within the Windows Script Component without using functions such as CreateObject(). Using an <object> element makes the object available with global scope and enables scripting tools to provide statement completion for the object's members.


<package> Element

Encloses multiple job definitions in a Windows Script Host control (.wsf) file.

<package>
   code for one or more jobs
</package>

Remarks

The <package> element is optional when a .wsf file contains only one job.

Example

The following example incorporates two jobs into one .wsf file using two different scripting languages:

<package>

<job id="DoneInVBS">
<?job debug="true"?>
<script language="VBScript">
   WScript.Echo "This is VBScript"
</script>
</job>

<job id="DoneInJS">
<?job debug="true"?>
<script language="JScript">
   WScript.Echo("This is JScript");
</script>
</job>

</package>


<reference> Element

XML element that includes a reference to an external type library.

<reference [object="progID"|guid="typelibGUID"] [version="version"] />

Arguments

progID

The program ID from which the type library can be derived. It can include either a version number (for example, ADO.Recordset.2.0), the explicit program ID of a type library, or the program ID of the executable (such as a .DLL) that incorporates the type library. If you use the object attribute, you do need not specify a version attribute, because the version can be inferred from the program ID. If the object attribute is specified, you cannot also specify a guid attribute.

typelibGUID

The GUID of the type library to reference. If the guid attribute is specified, you cannot also specify an object attribute.

version

(Optional) The version number of the type library to use. It must be in the form <major version>[.<minor version>]. If a version is not specified, the default version is 1.0. If the object attribute is used to specify the type library and the version is not specified, the version is derived from the Registry key for the specified program ID. If none can be found, the default is 1.0.

Remarks

Referencing a type library in your Windows Script Component enables you to use constants defined in the type library in scripts. The <reference> element looks up and makes available the type library associated with a specific program ID or type library name. Type library information can be available in .tlb, .olb, or .dll files.

Error Values

The WSH run-time engine can return one of the following typelib-related error messages:

Error | Description | Reason
WSH 0201: Invalid type library specification | The TYPELIB tag contains an invalid type library specification. | Typelib is corrupt.
WSH 0202: Type library cannot be loaded | Windows Script Host cannot load the type library specified in the TYPELIB tag. | Cannot find/load the typelib.
WSH 0203:Type library cannot be wrapped | Windows Script Host cannot create a Type Library Wrapper object from the type libraries specified in the TYPELIB tag. | Other.


<resource> Element

XML element that isolates textual or numeric data that should not be hard-coded into a script.

<resource id="resourceID">
   text or number
</resource>

Arguments

resourceID

A unique identifier for the resource within the script.

Remarks

The <resource> element allows you to isolate strings or numbers in your Windows script file that you want to reference in your scripts. For example, resource elements typically are used to maintain strings that may be localized into other languages.

To get the value of a resource, call the getResource method, passing it the ID of the resource you want to use.

Example

The following code fragment defines a resource (errNonNumeric) and returns its value if the variable upperBound is not numeric.

<resource id="errNonNumeric">
   Non-numeric value passed
</resource>

<script language="VBScript">
<![CDATA[
Function getRandomNumber(upperBound)
   If IsNumeric(upperBound) Then
      getRandomNumber = CInt(upperBound * Rnd + 1)
   Else
      getRandomNumber=getResource("errNonNumeric")
   End If
End Function
]]>
</script>


<script> Element

XML element that contains script to define the behavior of a Windows Script component.

<script language="language" [src="strFile"]>
     script here
</script>

Arguments

language

The name of the scripting language used in the Windows Script component file, such as Microsoft Visual Basic® Scripting Edition (VBScript) or Microsoft JScript®.

strFile

The name of the script file to include into the <script> block.

Remarks

If XML validation is not enabled, the XML parser ignores all lines inside the <script> element. However, if XML validation is enabled by including the <?XML?> declaration at the top of the script component file, the XML parser can misinterpret greater than (>), less than (<), and other symbols used in script as XML delimiters.

If you are creating a file that conforms closely to XML syntax, you must ensure that characters in your script element are not treated as XML-reserved characters. To do this, enclose the actual script in a <![CDATA[ ... ]]> section.

Note: Do not include a CDATA section unless you also include the <?XML?> declaration.

Example

The following example incorporates two jobs into one .wsf file using two different scripting languages:

<package>

<job id="DoneInVBS">
<?job debug="true"?>
<script language="VBScript">
   WScript.Echo "This is VBScript"
</script>
</job>

<job id="DoneInJS">
<?job debug="true"?>
<script language="JScript">
   WScript.Echo("This is JScript");
</script>
</job>

</package>


Windows Script Host Error List

Error Message | Possible Cause
The shortcut path name must end with .lnk or .url. | Attempting to create a shortcut with a bad file extension.
There is no printer called ""%1"". | Bad printer name in SetDefaultPrinter.
Unable to remove environment variable ""%1"". | Calling Environment.Remove on a nonexistent environment variable.
Unable to remove registry key ""%1"". | Calling RegDelete on a nonexistent registry key.
Invalid root in registry key ""%1"". | Calling RegRead or RegWrite on an invalid registry key.
Unable to open registry key ""%1"" for reading. | Calling RegRead on a nonexistent registry key.
Unable to write to wsh.log. Please check with your administrator. | Calling LogEvent on Win9x when the %windir%\wsh.log file is locked.
Unable to save shortcut ""%1"". | Attempting to save a shortcut or a URL shortcut that already exists and is read-only.
Protocol handler for ""%1"" failed to execute. | Attempting to set a URL shortcut target to an improperly registered protocol handler.
Invalid syntax in URL: ""%1"". | Attempting to set a URL shortcut target to a misspelled URL.
Protocol handler for ""%1"" not found. | Attempting to set a URL shortcut target waited on, such as waiting for the result from running a shortcut link. to a URL with a nonexistent protocol handler.
Unable to execute - argument list is too long. | Too many items dropped on a script file during a drag and drop operation.
Unable to wait for process. | Waiting on a process that cannot be


AddPrinterConnection Method

Maps the remote printer specified by strRemoteName to the local resource name strLocalName.

object.AddPrinterConnection strLocalName, strRemoteName[,bUpdateProfile][,strUser][,strPassword]

Arguments

object

WshNetwork object.

strLocalName

Local printer resource.

strRemoteName

Remote printer resource.

bUpdateProfile

Optional. If bUpdateProfile is supplied and its value is TRUE, this mapping is stored in the user profile.

strUser

Optional. If you are mapping a remote printer using the credentials of someone other than current user, you can specify strUser and strPassword.

strPassword

Optional. If you are mapping a remote printer using the credentials of someone other than current user, you can specify strUser and strPassword.

Example

The following example uses the AddPrinterConnection method to connect a network printer to LPT1.

Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.AddPrinterConnection "LPT1", "\\Server\Print1"

See Also

AddWindowsPrinterConnection Method | EnumPrinterConnections Method | RemovePrinterConnection Method | SetDefaultPrinter Method

Applies To: WshNetwork Object


AddWindowsPrinterConnection Method

Adds a printer connection to Windows.

Windows NT/Windows 2000:
object.AddWindowsPrinterConnection(
  strPrinterPath
)

Windows 95/98:
object.AddWindowsPrinterConnection(
  strPrinterPath,
  strDriverName[,strPort]
)

Arguments

object

WshNetwork object.

strPrinterPath

Path to the printer.

strDriverName

Name of the driver to use. Required on Windows 95/98; ignored if used on Windows NT/Windows 2000.

strPort

Optional. Specifies the port to which to attach the printer on Windows 95/98. Default is LPT1. Ignored if used on Windows NT/Windows 2000.

Remarks

Using this method is the same as using the Control Panel/Printers to add a printer connection. Unlike the AddPrinterConnection method, this method allows you to create a printer connection without directing it to a specific port such as LPT1. If the connection fails, an error occurs.

In Windows 95/98 the printer driver must already be installed on the machine in order for the AddWindowsPrinterConnection method to work. If the driver is not installed Windows will return the error message "Unknown printer driver".

Example

The following example uses the AddWindowsPrinterConnection method to connect a network printer to LPT1.

Set WshNetwork = CreateObject("WScript.Network")
Set PrinterPath = "\\printserv\DefaultPrinter"
Set PrinterDriver = "Lexmark Optra S 1650"
Set rc = WshNetwork.AddWindowsPrinterConnection(PrinterPath, PrinterDriver)
If Not rc then
      WScript.Echo("Printer Connection Failed!")
End If

See Also

Applies To: WshNetwork Object


AppActivate Method

Activates an application window.

AppActivate title

Arguments

title

Required. String expression specifying the title in the title bar of the application window that you want to activate. You can use the task ID returned by the Shell function in place of title to activate an application.

Remarks

The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes some action to change the focus or closes the window. Use the Shell function to start an application and set the window style.
In determining which application to activate, title is compared to the title string of each running application. If no exact match exists, any application whose title string begins with "title" is activated. If an application still is not found, any application whose title string ends with title is activated. If more than one instance of the application named by title exists, one instance is arbitrarily activated.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each runs the Windows calculator and send keystrokes to execute a simple calculation:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   WshShell.Run "calc"
   WScript.Sleep 100
   WshShell.AppActivate "Calculator"
   WScript.Sleep 100
   WshShell.SendKeys "1{+}"
   WScript.Sleep 500
   WshShell.SendKeys "2"
   WScript.Sleep 500
   WshShell.SendKeys "~"
   WScript.Sleep 500
   WshShell.SendKeys "*3"
   WScript.Sleep 500
   WshShell.SendKeys "~"
   WScript.Sleep 2500
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   WshShell.Run("calc");
   WScript.Sleep(100);
   WshShell.AppActivate("Calculator");
   WScript.Sleep(100);
   WshShell.SendKeys("1{+}");
   WScript.Sleep(500);
   WshShell.SendKeys("2");
   WScript.Sleep(500);
   WshShell.SendKeys("~");
   WScript.Sleep(500);
   WshShell.SendKeys("*3");
   WScript.Sleep(500);
   WshShell.SendKeys("~");
   WScript.Sleep(2500);
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShell Object


Close Method

Closes an open stream.

object.strStream.Close

Arguments

object

WScript object.

strStream

StdIn, StdOut, or StdErr property.

Remarks

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: StdErr Property | StdIn Property | StdOut Property


ConnectObject Method

Connects an object's event sources to functions with a given prefix.

object.ConnectObject strObject, strPrefix

Arguments

object

WScript object.

strObject

Name of the object to be connected.

strPrefix

Prefix of the event function.

Remarks

The object must be capable of enumerating its own class event set.

Example

The following example instantiates a fictitious object and connects it to the script file. The script then calls the "SomeMethod" method on the object. If an event called "Event" occurs in the object, the MyEvent_Event subroutine is called.

Set MyObject = WScript.CreateObject("SomeObject")
MyObject.SomeMethod
WScript.ConnectObject MyObject, "MyEvent_"

Sub MyEvent_Event(strName)
   WScript.Echo strName
End Sub

WScript.DisconnectObject MyObject

See Also

Applies To: WScript Object


CreateObject Method

Creates an object specified by the strProgID parameter.

object.CreateObject(strProgID[,strPrefix])

Arguments

object

WScript object.

strProgID

The program ID of the object to create.

strPrefix

Optional.

Remarks

If the parameter strPrefix is specified, Windows Script Host connects the object's outgoing interface to the script file after creating the object. When the object fires an event, Windows Script Host calls a subroutine whose name consists of strPrefix and the event name. For example, if strPrefix is "MYOBJ_" and the object fires an event named "OnBegin," Windows Script Host calls the "MYOBJ_OnBegin" subroutine located in the script.

Example

The following example uses the CreateObject method to create a WshNetwork object:

Set WshNetwork = WScript.CreateObject("WScript.Network")

See Also

GetObject Method | DisconnectObject Method

Applies To: WScript Object


CreateShortcut Method

Creates an object reference to a shortcut or URLshortcut.

object.CreateShortcut(strPathname)

Arguments

object

WshShell object.

strPathname

Path and file name of the shortcut being created.

Remarks

The CreateShortcut method creates a WshShortcut object and returns it. This method can be used to create a new shortcut or open an existing one to modify its properties. A Windows shortcut must have the extension ".lnk". If the shortcut title ends with ".url", a WshURLShortcut object is created.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

WshShortcut Object | WshUrlShortcut Object

Applies To: WshShell Object


DisconnectObject Method

Disconnects a previously connected object from Windows Script Host.

object.DisconnectObject obj

Arguments

object

WScript object.

obj

Name of the object to be disconnected.

Remarks

If the specified object is not already connected to Windows Script Host, this method does nothing.

Example

The following example creates an instance of a fictitious object and connects it to the script file. The script then calls the "SomeMethod" method on the object. If an error called "Event" occurs in the object, the MyEvent_Event subroutine is called.

Set MyObject = WScript.CreateObject("SomeObject", "MyEvent")
MyObject.SomeMethod
Sub MyEvent_Event(strName)
   WScript.Echo strName
End Sub
WScript.DisconnectObject MyObject

See Also

CreateObject Method | GetObject Method

Applies To: WScript Object


Echo Method

Sends output to a dialog box or the console.

object.Echo [Arg1] [,Arg2] [,Arg3] ...

Arguments

object

WScript object.

Arg1, Arg2, Arg3 ...

List of items to be displayed.

Remarks

This method displays the output in a dialog box if the host is WScript.exe, or to the console if it is CScript.exe. Each item will be separated by a single space. Under CScript.exe a newline character is placed at the end of output. If no items are provided a blank line will be displayed.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
      WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WScript Object


EnumNetworkDrives Method

Returns the current network drive mappings.

objDrives = object.EnumNetworkDrive

Arguments

object

WshNetwork object.

objDrives

Variable reference to the collection of drives.

Remarks

Items in this collection are local names and remote names. The first item in the collection is the drive letter and the second item is the network mapping. This pattern continues for all the network drives in the collection.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
      WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

MapNetworkDrive Method | RemoveNetworkDrive Method

Applies To: WshNetwork Object


EnumPrinterConnections Method

Returns the current network printer mappings.

objPrinters = object.EnumPrinterConnections

Arguments

object

WshNetwork object.

objPrinters

Variable reference to the collection of printers.

Remarks

Items in this collection are local names and remote names. The first item in the collection is the port and the second item is the network mapping. This pattern continues for all the network printers in the collection.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
      WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

AddPrinterConnection Method | AddWindowsPrinterConnection Method | RemovePrinterConnection Method | SetDefaultPrinter Method

Applies To: WshNetwork Object


ExpandEnvironmentStrings Method

Expands the requested environment variable from the running process and returns the result string.

object.ExpandEnvironmentStrings(strString)

Arguments

object

WshShell object.

strString

Name of the environment variable to expand.

Remarks

Variables are enclosed by the "%" character. The environment variable name is not case-sensitive.

Example

The following example expands the WinDir environment variable and displays it in a message box:

set WshShell = CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%WinDir%")

See Also

Applies To: WshShell Object


GetObject Method

Retrieves an Automation object from a file or an object specified by the strProgID parameter.

object.GetObject(strPathname [,strProgID], [strPrefix])

Arguments

object

WScript object.

strPathname

Required. The full path and name of the file containing the object to retrieve. is required.

strProgID

Optional. A string representing the object's program identifier (ProgID).

strPrefix

Optional. If the parameter strPrefix is specified, Windows Script Host connects the object's outgoing interface to the script file after creating the object. When the object fires an event, Windows Script Host calls a subroutine whose name consists of strPrefix and the event name.

For example, if strPrefix is "MYOBJ_" and the object fires an event named "OnBegin," Windows Script Host calls the "MYOBJ_OnBegin" subroutine located in the script.

Remarks

Use the GetObject method when there is a current instance of the object or if you want to create the object from a file that is already loaded. If no current instance exists and if you do not want the object started from a file that is already loaded, use the CreateObject method.

If an object has registered itself as a single-instance object (for example, the Word.Basic object in Microsoft Word 7.0), only one instance of the object is created, no matter how many times CreateObject is executed. In addition, with a single-instance object, GetObject always returns the same instance when called with the zero-length string syntax (""), and it causes an error if the path parameter is omitted. You cannot use GetObject to obtain a reference to a Microsoft Visual Basic® class created with Visual Basic 4.0 or earlier.

GetObject works with all COM classes, independent of the language used to create the object.

Example

When the following code is executed, the application associated with the specified strPathname is started, and the object in the specified file is activated. If strPathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the strPathname parameter is omitted entirely, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

Dim MyObject As Object
Set MyObject = GetObject("C:\CAD\SCHEMA.CAD")
MyApp = MyObject.Application

Some applications allow you to activate part of a file. To do this, add an exclamation point (!) to the end of the file name, and follow it with a string that identifies the part of the file you want to activate. For information on how to create this string, see the documentation for the application that created the object.

For example, in a drawing application, a drawing stored in a file might have multiple layers. You could use the following code to activate a layer within a drawing file called schema.cad:

Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")

If you do not specify the object's class, COM determines the application to start and the object to activate according to the file name you provide. Some files, however, may support more than one class of object. For example, a drawing might support three different types of objects: an application object, a drawing object, and a toolbar object, all of which are part of the same file.

In the following example, FIGMENT is the name of a drawing application, and DRAWING is one of the object types it supports.

Dim MyObject As Object
Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING")

See Also

CreateObject Method | DisconnectObject Method

Applies To: WScript Object


getResource Method

Returns the value of a resource defined with the <resource> element.

getResource(resourceID)

Arguments

resourceID

A unique identifier for the resource within the script.

Remarks

The <resource> element allows you to isolate strings or numbers in your Windows script file that you want to reference in your scripts. For example, resource elements typically are used to maintain strings that may be localized into other languages. You can use the getResource method in the script to extract the contents of a <resource> element.

Example

The following code fragment defines a resource (errNonNumeric) and returns its value if the variable upperBound is not numeric.

<resource id="errNonNumeric">
      Non-numeric value passed
</resource>

<script language="VBScript">
<![CDATA[
Function getRandomNumber(upperBound)
      If IsNumeric(upperBound) Then
            getRandomNumber = CInt(upperBound * Rnd + 1)
      Else
            getRandomNumber=getResource("errNonNumeric")
      End If
End Function
]]>
</script>


LogEvent Method

Logs an event in the Windows NT event log or WSH.log file.

object.LogEvent(intType, strMessage [,strTarget])

Arguments

object

WshShell object.

intType

Number representing the type of the event.

strMessage

The text of the log entry.

strTarget

Optional, Windows NT only. The name of the system where the event should be logged. Default is local system. Parameter is ignored on Windows 9x.

Remarks

LogEvent types may be one of the following:

Type | Value
0 | SUCCESS
1 | ERROR
2 | WARNING
4 | INFORMATION
8 | AUDIT_SUCCESS
16 | AUDIT_FAILURE

On Windows NT: Logs an event in the Windows NT event log.

On Windows 9x: Logs an event in WSH.log, which will be in the users Windows directory. The WSH.log file will contain a time stamp, the event type, and the text of the log entry.

Returns true if the event is successfully logged, otherwise returns false.

Example

The following example logs if a logon was successful or not:

Set WshShell = WScript.CreateObject("WScript.Shell")
rc = runLoginScript()      'Returns true if logon succeeds
if rc then
      WshShell.LogEvent 0, "Logon Script Completed Successfully"
else
      WshShell.LogEvent 1, "Logon Script failed"
end if

See Also

Applies To: WshShell Object


MapNetworkDrive Method

Maps the share point specified by strRemoteName to the local resource name strLocalName.

object.MapNetworkDrive strLocalName, strRemoteName, [bUpdateProfile], [strUser], [strPassword]

Arguments

object

WshNetwork object.

strLocalName

Local resource name.

strRemoteName

Remote share to map.

bUpdateProfile

Optional. If bUpdateProfile is supplied and its value is TRUE, the mapping is stored in the user profile. The default is FALSE.

strUser, strPassword

Optional. If you are mapping the share point using the credentials of someone other than current user, you can specify strUser and strPassword.

Example

The following example shows how to map the "Z" drive to a network share:

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\Server\Share"

See Also

EnumNetworkDrives Method | RemoveNetworkDrive Method

Applies To: WshNetwork Object


Popup Method

Displays a pop-up message box window that contains the message contained in strText.

intButton = object.Popup(
  strText,
  [natSecondsToWait],
  [strTitle],
  [natType]
)

Arguments

object

WshShell object.

strText

The text to appear in the pop-up window.

natSecondsToWait

Optional. The number of seconds to wait before closing the pop-up window.

strTitle

Optional. The text to appear in the title of the pop-up window.

natType

Optional. The type of buttons and icons to display in the pop-up window.

Remarks

The window title of this message box is specified by strTitle. If strTitle is omitted, the window title is "Windows Script Host."

If natSecondsToWait is supplied and its value is greater than zero, the message box window will be closed after natSecondsToWait seconds.

The meaning of natType is the same as in the Microsoft Win32® application programming interface MessageBox function. The following tables show the values and their meanings. You can combine values in these tables.

Button Types

Value | Description
0 | Show OK button.
1 | Show OK and Cancel buttons.
2 | Show Abort, Retry, and Ignore buttons.
3 | Show Yes, No, and Cancel buttons.
4 | Show Yes and No buttons.
5 | Show Retry and Cancel buttons.

Icon Types

Value | Description
16 | Show "Stop Mark" icon.
32 | Show "Question Mark" icon.
48 | Show "Exclamation Mark" icon.
64 | Show "Information Mark" icon.

The previous two tables do not cover all values for natType. For a complete list, see the Microsoft Win32 documentation.

The return value intButton denotes the number of the button that the user clicked. If the user does not click a button before natSecondsToWait seconds, intButton is set to -1.

Value | Description
1 | OK button
2 | Cancel button
3 | Abort button
4 | Retry button
5 | Ignore button
6 | Yes button
7 | No button

Example

The following example generates a simple pop-up window:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Popup "Where do you want to go today?"

See Also

Echo Method

Applies To: WshShell Object


Quit Method

Quits execution with a specified error code.

object.Quit [intErrorCode]

Arguments

object

WScript object.

intErrorCode

Optional. If this parameter is included, WScript returns it as the process exit code. If intErrorCode is omitted, WScript returns zero (0) as the process exit code.

Example

The following example quits execution and returns an error code of 1:

WScript.Quit 1

See Also

Applies To: WScript Object


Read Method

Reads a specified number of characters from an input stream and returns the resulting string.

object.strStream.Read(characters)

Arguments

object

WScript object.

strStream

StdIn property.

characters

Number of characters you want to read from the stream.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdIn Property


ReadAll Method

Reads an entire input stream and returns the resulting string.

object.strStream.ReadAll

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdIn Property


ReadLine Method

Reads an entire line (up to, but not including, the newline character) from an input stream and returns the resulting string.

object.strStream.ReadLine

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdIn Property


RegDelete Method

Deletes from the registry the key or value named by strName.

object.RegDelete strName

Arguments

object

WshShell object.

strName

Key or value name to delete.

Remarks

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
HKCU | HKEY_CURRENT_USER
HKLM | HKEY_LOCAL_MACHINE
HKCR | HKEY_CLASSES_ROOT
 | HKEY_USERS
 | HKEY_CURRENT_CONFIG

Example

The following example deletes a value and a key from the registry:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegDelete "HKCU\ScriptEngine\Value" ' Delete value "Value".
WshShell.RegDelete "HKCU\ScriptEngine\Key\"  ' Delete key "Key".

See Also

RegRead Method | RegWrite Method

Applies To: WshShell Object


RegRead Method

Returns the registry key or value named by strName.

object.RegRead(strName)

Arguments

object

WshShell object.

strName

Key or value name to read.

Remarks

If strName ends with the backslash character (\), this method returns the key instead of the value. StrName must begin with one of following root key names:

Short | Long
HKCU | HKEY_CURRENT_USER
HKLM | HKEY_LOCAL_MACHINE
HKCR | HKEY_CLASSES_ROOT
 | HKEY_USERS
 | HKEY_CURRENT_CONFIG

The RegRead method supports only REG_SZ, REG_EXPAND_SZ, REG_DWORD, REG_BINARY, and REG_MULTI_SZ data types. If the registry has other data types, RegRead returns DISP_E_TYPEMISMATCH.

Example

The following example reads a value and key from the registry:

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.RegRead("HKCU\ScriptEngine\Val")   ' Read from value "Val".
WScript.Echo WshShell.RegRead("HKCU\ScriptEngine\Key\") ' Read from key "Key".

See Also

RegDelete Method | RegWrite Method

Applies To: WshShell Object


RegWrite Method

Sets the registry key or value named by strName.

object.RegWrite strName, anyValue [,strType]

Arguments

object

WshShell object.

strName

Key or value name to write.

anyValue

The value to write into the key or registry value.

strType

Optional. The data type for the value being stored in the registry.

Remarks

If strName ends with the backslash character (\), this method returns the key instead of the value. StrName must begin with one of following root key names:

Short | Long
HKCU | HKEY_CURRENT_USER
HKLM | HKEY_LOCAL_MACHINE
HKCR | HKEY_CLASSES_ROOT
 | HKEY_USERS
 | HKEY_CURRENT_CONFIG

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.

RegWrite automatically converts anyValue to a string when strType is REG_SZ or REG_EXPAND_SZ. If strType is REG_DWORD, anyValue is converted to an integer. If strType is REG_BINARY, anyValue must be an integer.

Example

The following example writes a value and key entry into the registry:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU\ScriptEngine\Value", "Some string value"
WshShell.RegWrite "HKCU\ScriptEngine\Key\", 1 ,"REG_DWORD"

See Also

RegDelete Method | RegRead Method

Applies To: WshShell Object


Remove Method

Deletes the environment variable specified by strName.

object.Remove(strName)

Arguments

object

WshEnvironment object.

strName

The name of the environment variable to delete.

Example

The following example deletes the EXAMPLE volatile environment variable and then deletes multiple variables from the User environment:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Environment("VOLATILE").Remove("EXAMPLE")

Set WshUsrEnv = WScript.Environment("User")
WshUsrEnv.Remove("EXAMPLE_1")
WshUsrEnv.Remove("EXAMPLE_2")
WshUsrEnv.Remove("EXAMPLE_3")
WshUsrEnv.Remove("EXAMPLE_4")

See Also

Environment Property

Applies To: WshEnvironment Object


RemoveNetworkDrive Method

Removes the current resource connection denoted by strName.

object.RemoveNetworkDrive strName, [bForce], [bUpdateProfile]

Arguments

object

WshNetwork object.

strName

The strName parameter can be either a local name or a remote name, depending on how the drive is mapped. If the drive has a mapping between a local name (drive letter) and a remote name, then strName must be set to the local name. If the network path does not have a local name (drive letter) mapping, then strName must be set to the remote name.

bForce

Optional. If bForce is supplied and its value is TRUE, this method removes the connections whether the resource is used or not.

bUpdateProfile

Optional. If bUpdateProfile is supplied and its value is TRUE, this mapping is removed from the user profile.

Remarks

The following example maps a network share to drive "Z" and removes the connection:

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\Server\Share"
WshNetwork.RemoveNetworkDrive "Z:"

See Also

EnumNetworkDrives Method | MapNetworkDrive Method

Applies To: WshNetwork Object


RemovePrinterConnection Method

Removes the current resource connection denoted by strName.

object.RemovePrinterConnection strName, [bForce], [bUpdateProfile]

Arguments

object

WshNetwork object.

strName

The strName parameter can be either a local name or a remote name, depending on how the printer is connected. If the printer has a mapping between a local name (for example, LPT1) and a remote name, then strName must be set to the local name. If the network path does not have a local name mapping, then strName must be set to the remote name.

bForce

Optional. If bForce is supplied and its value is TRUE, this method removes the connections whether the resource is used or not.

bUpdateProfile

Optional. If bUpdateProfile is supplied and its value is TRUE, this mapping is removed from the user profile.

Example

The following example removes a printer connection from LPT1:

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "LPT1:"

See Also

AddPrinterConnection Method | AddWindowsPrinterConnection Method | EnumPrinterConnections Method | SetDefaultPrinter Method

Applies To: WshNetwork Object


Run Method

Creates a new process that executes strCommand.

object.Run (strCommand, [intWindowStyle], [bWaitOnReturn])

Arguments

object

WshShell object.

strCommand

Environment variables within the strCommand parameter are automatically expanded.

intWindowStyle

Optional. Sets the window style of the program being run.

bWaitOnReturn

Optional. If bWaitOnReturn is not specified or FALSE, immediately returns to script execution rather than waiting for the process to end.

If bWaitOnReturn is set to TRUE, the Run method returns any error code returned by the application.

If bWaitOnReturn is not specified or is FALSE, the Run method returns an error code of 0 (zero).

Remarks

The following table lists the available settings for intWindowStyle:

intWindowStyle | Description
0 | Hides the window and activates another window.
1 | Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 | Activates the window and displays it as a minimized window.
3 | Activates the window and displays it as a maximized window.
4 | Displays a window in its most recent size and position. The active window remains active.
5 | Activates the window and displays it in its current size and position.
6 | Minimizes the specified window and activates the next top-level window in the Z order.
7 | Displays the window as a minimized window. The active window remains active.
8 | Displays the window in its current state. The active window remains active.
9 | Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 | Sets the show state based on the state of the program that started the application.

Example

The following example runs Microsoft Notepad and opens the currently running script:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run ("%windir%\notepad" & WScript.ScriptFullName)

The following example returns the error code from the executed application:

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, TRUE)

See Also

Applies To: WshShell Object


Save Method

Saves a shortcut to the specified location.

object.Save

Arguments

object

WshShortcut or WshUrlShortcut object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShortcut Object | WshUrlShortcut Object


SendKeys Method

Sends one or more keystrokes to the active window as if typed at the keyboard.

SendKeys string

Arguments

string

Required. String expression specifying the keystrokes to send.

Remarks

Each key is represented by one or more characters. To specify a single keyboard character, use the character itself. For example, to represent the letter A, use "A" for string. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, use "ABC" for string.
The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that may be significant when dynamic data exchange (DDE) occurs. To specify brace characters, use {{} and {}}.

To specify characters that aren't displayed when you press a key, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes shown below:

Key | Code
BACKSPACE | {BACKSPACE}, {BS}, or {BKSP}
BREAK | {BREAK}
CAPS LOCK | {CAPSLOCK}
DEL or DELETE | {DELETE} or {DEL}
DOWN ARROW | {DOWN}
END | {END}
ENTER | {ENTER}or ~
ESC | {ESC}
HELP | {HELP}
HOME | {HOME}
INS or INSERT | {INSERT} or {INS}
LEFT ARROW | {LEFT}
NUM LOCK | {NUMLOCK}
PAGE DOWN | {PGDN}
PAGE UP | {PGUP}
PRINT SCREEN | {PRTSC}
RIGHT ARROW | {RIGHT}
SCROLL LOCK | {SCROLLLOCK}
TAB | {TAB}
UP ARROW | {UP}
F1 | {F1}
F2 | {F2}
F3 | {F3}
F4 | {F4}
F5 | {F5}
F6 | {F6}
F7 | {F7}
F8 | {F8}
F9 | {F9}
F10 | {F10}
F11 | {F11}
F12 | {F12}
F13 | {F13}
F14 | {F14}
F15 | {F15}
F16 | {F16}

To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes:

Key | Code
SHIFT | +
CTRL | ^
ALT | %

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times.

Note You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows. Sendkeys also can't send the PRINT SCREEN key {PRTSC} to any application.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each runs the Windows calculator and send keystrokes to execute a simple calculation:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   WshShell.Run "calc"
   WScript.Sleep 100
   WshShell.AppActivate "Calculator"
   WScript.Sleep 100
   WshShell.SendKeys "1{+}"
   WScript.Sleep 500
   WshShell.SendKeys "2"
   WScript.Sleep 500
   WshShell.SendKeys "~"
   WScript.Sleep 500
   WshShell.SendKeys "*3"
   WScript.Sleep 500
   WshShell.SendKeys "~"
   WScript.Sleep 2500
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   WshShell.Run("calc");
   WScript.Sleep(100);
   WshShell.AppActivate("Calculator");
   WScript.Sleep(100);
   WshShell.SendKeys("1{+}");
   WScript.Sleep(500);
   WshShell.SendKeys("2");
   WScript.Sleep(500);
   WshShell.SendKeys("~");
   WScript.Sleep(500);
   WshShell.SendKeys("*3");
   WScript.Sleep(500);
   WshShell.SendKeys("~");
   WScript.Sleep(2500);
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShell Object


Skip Method

Skips a specified number of characters when reading an input stream.

object.strStream.Skip(characters)

Arguments

object

WScript object.

strStream

StdIn property.

characters

Number of characters to skip when reading an input stream.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdIn Property


SkipLine Method

Skips the next line when reading an input stream.

object.strStream.SkipLine

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdIn Property


Sleep Method

Places the script process into an inactive state for the number of milliseconds specified and then continues execution.

object.Sleep(intTime)

Arguments

object

WScript object.

intTime

Amount of time in milliseconds that the script process will be inactive.

Remarks

Events handled by the script will still be executed during a sleep. If the script engine times out or is otherwise interrupted the sleep will terminate immediately.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each runs the Windows calculator and send keystrokes to execute a simple calculation:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   WshShell.Run "calc"
   WScript.Sleep 100
   WshShell.AppActivate "Calculator"
   WScript.Sleep 100
   WshShell.SendKeys "1{+}"
   WScript.Sleep 500
   WshShell.SendKeys "2"
   WScript.Sleep 500
   WshShell.SendKeys "~"
   WScript.Sleep 500
   WshShell.SendKeys "*3"
   WScript.Sleep 500
   WshShell.SendKeys "~"
   WScript.Sleep 2500
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   WshShell.Run("calc");
   WScript.Sleep(100);
   WshShell.AppActivate("Calculator");
   WScript.Sleep(100);
   WshShell.SendKeys("1{+}");
   WScript.Sleep(500);
   WshShell.SendKeys("2");
   WScript.Sleep(500);
   WshShell.SendKeys("~");
   WScript.Sleep(500);
   WshShell.SendKeys("*3");
   WScript.Sleep(500);
   WshShell.SendKeys("~");
   WScript.Sleep(2500);
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WScript Object


Write Method

Writes a specified string to an output stream.

object.strStream.Write(strText)

Arguments

object

WScript object.

strStream

StdOut or StdErr property.

strText

The text you want to write to the stream.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdErr Property | StdOut Property


WriteBlankLines Method

Writes a specified number of newline characters to an output stream.

object.strStream.WriteBlankLines(intLines)

Arguments

object

WScript object.

strStream

StdOut or StdErr property.

intLines

Number of newline characters to write to the stream.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.

See Also

Applies To: StdErr Property | StdOut Property


WriteLine Method

Writes a specified string and newline character to an output stream.

object.strStream.WriteLine([strText])

Arguments

object

WScript object.

strStream

StdOut or StdErr property.

strText

Optional. The text you want to write to the stream. If omitted, a newline character is written to the stream.

Remarks

The StdIn, StdOut and StdErr properties and methods will only work when running the script with CScript.exe. An "Invalid Handle" error will be returned when run with WScript.exe.


WScript Object

Exposes properties that provide the path of the running scripting host (WScript.exe or cscript.exe), its arguments, and the working mode (interactive or batch). The WScript object also provides methods to create and read objects.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

Properties

Application Property | Arguments Property | FullName Property | Name Property | Path Property | ScriptFullName Property | ScriptName Property | StdErr Property | StdIn Property | StdOut Property | Version Property

Methods

CreateObject Method | ConnectObject Method | DisconnectObject Method | Echo Method | GetObject Method | Quit Method | Sleep Method

See Also

WshShell Object


WshArguments Object

Returns a pointer to the collection of command-line parameters.

Remarks

Not exposed; accessed through the Arguments property.

Example

The following example displays all command-line parameters in the Arguments collection:

Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   WScript.Echo objArgs(I)
Next

Properties

Item Property | Count Property | Length Property

See Also

Arguments Property


WshEnvironment Object

Retrieves the Microsoft Windows system environment variables.

Remarks

Not exposed; accessed through the Environment property.

Example

The following example retrieves the NUMBER_OF_PROCESSORS system environment variable.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")

Properties

Item Property | Count Property | Length Property

Methods

Remove Method

See Also

Environment Property


WshNetwork Object

Exposes the Microsoft Windows network, making it easy to connect and disconnect remote drives and printers.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
   WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

Properties

ComputerName Property | UserDomain Property | UserName Property

Methods

AddPrinterConnection Method | EnumNetworkDrives Method | EnumPrinterConnection Method | MapNetworkDrive Method | RemoveNetworkDrive Method | RemovePrinterConnection Method | SetDefaultPrinter Method


WshShell Object

Starts a new process, creates shortcuts, and provides the Environment collection to handle environmental variables such as WINDIR, PATH, or PROMPT.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

Properties

Environment Property | SpecialFolders Property

Methods

AppActivate Method | CreateShortcut Method | ExpandEnvironmentStrings Method | LogEvent Method | Popup Method | RegDelete Method | RegRead Method | RegWrite Method | Run Method | SendKeys Method


WshShortcut Object

Creates an object reference to a shortcut.

Remarks

Not exposed; accessed through the CreateShortcut method.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

Properties

Arguments Property | Description Property | FullName Property | Hotkey Property | IconLocation Property | TargetPath Property | WindowStyle Property | WorkingDirectory Property

Methods

Save Method

See Also

CreateShortcut Method


WshSpecialFolders Object

Returns the paths for Windows shell folders such as the desktop folder, Start menu folder, and personal document folder.

Remarks

Not exposed; accessed through the SpecialFolders property. Can be used to get the location of the desktop folder in order to create shortcuts on the desktop.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

Properties

Item Property | Count Property | Length Property

See Also

SpecialFolders Property


WshUrlShortcut Object

Creates an object reference to a URLshortcut.

Remarks

Not exposed; accessed through the CreateShortcut method.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

Properties

FullName Property | TargetPath Property

Methods

Save Method

See Also

CreateShortcut Method


Application Property

Provides the IDispatch interface on the WScript object.

object.Application

Arguments

object

WScript object.

See Also

Applies To: WScript Object


Arguments Property

Returns a pointer to the WshArguments collection or identifies arguments for the shortcut to the collection.

object.Arguments

Arguments

object

WScript or WshShortcut object.

Example

The following example displays all command-line parameters in the Arguments collection:

Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   WScript.Echo objArgs(I)
Next

See Also

Applies To: WshShortcut Object | WScript Object | WshArguments Object


AtEndOfLine Property

Returns True if the stream pointer immediately precedes the end-of-line marker in an input stream; False if it does not.

object.strStream.AtEndOfLine

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: StdIn Property


AtEndOfStream Property

Returns True if the stream pointer is at the end of an input stream; False if it is not.

object.strStream.AtEndOfStream

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: StdIn Property


Column Property

Returns the column number of the current character position in an input stream.

object.strStream.Column

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

This property is read-only. After a newline character has been written, but before any other character is written, Column is equal to 1.

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: StdIn Property


ComputerName Property

Provides a string representation of the computer name.

object.ComputerName

Arguments

object

WshNetwork object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
      WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshNetwork Object


Count Property

Returns the number of enumerated items.

object.Count

Arguments

object

The result of the EnumNetworkDrive or EnumPrinterConnections methods, or the object returned by the Arguments property.

Remarks

In JScript, the Count property is implemented as a method, not as a property (as it is in VBScript). Because of this, you must add a set of parentheses at the end of the statement when using Count in JScript, as in the following example:

objArgs.Count()

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
   WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Arguments Property | Length Property

Applies To: EnumNetworkDrives Method | EnumPrinterConnections Method | Arguments Property


Description Property

Provides a description of a shortcut object.

object.Description

Arguments

object

WshShortcut object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShortcut Object


Environment Property

Returns the WshEnvironment object.

object.Environment ([strType])

Arguments

object

WshShell object.

strType

Optional. Specifies where the environment variable resides.

Remarks

If strType is supplied, it specifies where the environment variable resides, with a possible value of "System," "User," "Volatile," or "Process." If strType is not supplied, this method retrieves the system environment variables in Microsoft Windows 2000 or the process environment variables in Windows 95/98. For Windows 95/98, only "Process" is supported in the strType parameter.

The following table lists some of the variables that are provided with the Windows operating system. Scripts also can get environment variables that have been set by other applications. None of these variables are available from the "Volatile" type.

 |  | Windows 2000 |  |  | Windows 95/98
Name | Description | System | User | Process | Process
NUMBER_OF_PROCESSORS | Number of processors running on the machine. | X | - | X | -
PROCESSOR_ARCHITECTURE | Processor type of the user's workstation. | X | - | X | -
PROCESSOR_IDENTIFIER | Processor ID of the user's workstation. | X | - | X | -
PROCESSOR_LEVEL | Processor level of the user's workstation. | X | - | X | -
PROCESSOR_VERSION | Processor version of the user's workstation. | X | - | X | -
OS | Operating system on the user's workstation. | X | - | X | -
COMSPEC | Executable file for the command prompt (typically cmd.exe). | X | - | X | X
HOMEDRIVE | Primary local drive (typically the C drive). | - | - | X | -
HOMEPATH | Default directory for users (Typically \users\default in Windows 2000). | - | - | X | -
PATH | PATH environment variable. | X | X | X | X
PATHEXT | Extensions for executable files (typically .com, .exe, .bat, or .cmd). | X | - | X | -
PROMPT | Command prompt (typically $P$G). | - | - | X | X
SYSTEMDRIVE | Local drive on which the system directory resides (typically c:\). | - | - | X | -
SYSTEMROOT | System directory (for example, c:\winnt). This is the same as WINDIR. | - | - | X | -
WINDIR | System directory (for example, c:\winnt). This is the same as SYSTEMROOT. | X | - | X | X
TEMP | Directory for storing temporary files (for example, c:\temp). | - | X | X | X
TMP | Directory for storing temporary files (for example, c:\temp). | - | X | X | X

Example

The following example retrieves 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

Applies To: WshShell Object


FullName Property

Returns a string containing the full path to the host executable file or shortcut object.

object.FullName

Arguments

object

WScript, WshShortcut or WshUrlShortcut object.

Example

The following example returns the path of the host executable:

WScript.Echo WScript.Fullname

See Also

Path Property

Applies To: WScript Object | WshShortcut Object | WshUrlShortcut Object


Hotkey Property

Provides the hotkey of a shortcut object. A hotkey is a keyboard shortcut that starts or switches to a program.

object.Hotkey = strHotkey

Arguments

object

WshShortcut object.

strHotkey

Sequence of keys to assign.

Remarks

The syntax of strHotkey is as follows, with Hotkey being case-insensitive:

Arguments

modifier

"ALT+" | "CTRL+" | "SHIFT+" | "EXT+"

keyname

"A" .. "Z" |
"0".. "9" |
"Back" | "Tab" | "Clear" | "Return" |
"Escape" | "Space" | "Prior" | ...

Hotkeys can activate only shortcuts located on the Windows-based desktop or the Windows Start menu.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

WshSpecialFolders Object

Applies To: WshShortcut Object


IconLocation Property

Provides the icon location of a shortcut object.

object.IconLocation = strIconLocation

Arguments

object

WshShortcut object.

strIconLocation

Location and index of the icon. The format of the icon location should be "Path, index".

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShortcut Object


Item Property

Returns a specified item from a collection.

object.Item(natIndex)

Arguments

object

The result of the EnumNetworkDrive or EnumPrinterConnections methods, or the object returned by the Environment or SpecialFolders properties.

natIndex

Item to retrieve.

Remarks

Item is the default property for each collection. For EnumNetworkDrive and EnumPrinterConnections collections, index is an integer; for the Environment and SpecialFolders collections, index is a string.

WshShell.SpecialFolders.Item("strFolderName") returns NULL if the requested folder (strFolderName) is not available. For example, Windows 95/98 does not have an AllUsersDesktop folder, and returns NULL if strFolderName = AllUsersDesktop.

The following table lists special folders and the version of Windows in which they are provided:

Windows Special folders

Folder | Windows version
AllUsersDesktop | Windows 2000
AllUsersStartMenu | Windows 2000
AllUsersPrograms | Windows 2000
AllUsersStartup | Windows 2000
Desktop | Windows 95/98, Windows 2000
Favorites | Windows 95/98, Windows 2000
Fonts | Windows 95/98, Windows 2000
My Documents | Windows 95/98, Windows 2000
NetHood | Windows 95/98, Windows 2000
PrintHood | Windows 95/98, Windows 2000
Programs | Windows 95/98, Windows 2000
Recent | Windows 95/98, Windows 2000
SendTo | Windows 95/98, Windows 2000
Start Menu | Windows 95/98, Windows 2000
StartupB | Windows 2000
Templates | Windows 2000

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
      WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
   WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: EnumNetworkDrive Method | EnumPrinterConnections Method | Environment Property | SpecialFolders Property


Length Property

Returns the number of enumerated items.

object.length

Arguments

object

WshArguments, WshEnvironment, or WshSpecialFolders object.

Remarks

This property provides the same functionality as the Count property and is included for compatibility with Microsoft® JScript®.

See Also

Arguments Property | Count Property

Applies To: WshEnvironment Object | WshArguments Object | WshSpecialFolders Object


Line Property

Returns the current line number in an input stream.

object.strStream.Line

Arguments

object

WScript object.

strStream

StdIn property.

Remarks

This property is read-only. After a stream is initially opened, and before anything is written, Line is equal to 1.

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: StdIn Property


Name Property

Returns a string containing the friendly name of the WScript object.

object.Name

Arguments

object

WScript object.

Remarks

This is the default property and is read only.

Example

The following example shows two ways to use this property:

WScript.Echo WScript.Name
WScript.Echo WScript

See Also

Applies To: WScript Object


Path Property

Provides a string containing the name of the directory where WScript.exe or CScript.exe resides.

object.Path

Arguments

object

WScript object.

Example

The following example echoes the directory where the executable file resides:

WScript.Echo WScript.Path

See Also

FullName Property

Applies To: WScript Object


ScriptFullName Property

Provides the full path to the script currently being run.

object.ScriptFullName

Arguments

object

WScript object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

ScriptName Property

Applies To: WScript Object


ScriptName Property

Provides the file name of the script currently being run.

object.ScriptName

Arguments

object

WScript object.

Example

The following example echoes the name of the script being run:

WScript.Echo WScript.ScriptName

See Also

ScriptFullName Property

Applies To: WScript Object

See Also

©2000 Microsoft Corporation. All rights reserved.


SpecialFolders Property

Provides the WshSpecialFolders object for accessing Windows shell folders such as the desktop folder, the Start menu folder, and the personal document folder.

object.SpecialFolders(objWshSpecialFolders)

Arguments

object

WshShell object.

objWshSpecialFolders

The name of the special folder to return.

Remarks

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 Microsoft Windows NT 4.0 operating systems:

AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

WshSpecialFolders Object

Applies To: WshShell Object


StdErr Property

Exposes the write-only error output stream for the current script.

object.StdErr

Arguments

object

WScript object.

Remarks

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: WScript Object


StdIn Property

Exposes the read-only input stream for the current script.

object.StdIn

Arguments

object

WScript object.

Remarks

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: WScript Object


StdOut Property

Exposes the write-only output stream for the current script.

object.StdOut

Arguments

object

WScript object.

Remarks

The StdIn, StdOut, and StdErr properties and methods will work only when the script is run with CScript.exe. If the script is run with WScript.exe, an "Invalid Handle" error will result. For more information, see Windows Script Host Error Messages.

See Also

Applies To: WScript Object


TargetPath Property

Sets the path to a shortcut object's executable file.

object.TargetPath

Arguments

object

WshShortcut or WshUrlShortcut object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShortcut Object | WshUrlShortcut Object


UserDomain Property

Provides a string representation of the user domain name.

object.UserDomain

Arguments

object

WshNetwork object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
   WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
   WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshNetwork Object


UserName Property

Provides a string representation of the user name.

object.UserName

Arguments

object

WshNetwork object.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each displays domain name, computer name and user name for the current machine, and displays network mapping information for the drives and printers:

<package>

<job id="vbs">
<script language="VBScript">
   Set WshNetwork = WScript.CreateObject("WScript.Network")
   Set oDrives = WshNetwork.EnumNetworkDrives
   Set oPrinters = WshNetwork.EnumPrinterConnections
   WScript.Echo "Domain = " & WshNetwork.UserDomain
   WScript.Echo "Computer Name = " & WshNetwork.ComputerName
   WScript.Echo "User Name = " & WshNetwork.UserName
   WScript.Echo
   WScript.Echo "Network drive mappings:"
   For i = 0 to oDrives.Count - 1 Step 2
      WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
   Next
   WScript.Echo
   WScript.Echo "Network printer mappings:"
   For i = 0 to oPrinters.Count - 1 Step 2
   WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
   Next
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshNetwork = new ActiveXObject("WScript.Network");
   var oDrives = WshNetwork.EnumNetworkDrives();
   var oPrinters = WshNetwork.EnumPrinterConnections();
   WScript.Echo("Domain = " + WshNetwork.UserDomain);
   WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
   WScript.Echo("User Name = " + WshNetwork.UserName);
   WScript.Echo();
   WScript.Echo("Network drive mappings:");
   for(i=0; i<oDrives.Count(); i+=2){
      WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
   }
   WScript.Echo();
   WScript.Echo("Network printer mappings:");
   for(i=0; i<oPrinters.Count(); i+=2){
   WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
   }
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshNetwork Object


Version Property

Returns the version of Microsoft Windows Script Host.

object.Version

Arguments

object

WScript object.

Example

The following example echoes the current version of Windows Script Host:

WScript.Echo WScript.Version

See Also

Applies To: WScript Object


WindowStyle Property

Provides the window style of a shortcut object.

object.WindowStyle = intWindowStyle

Arguments

object

WshShortcut object.

intWindowStyle

Sets the window style of the program being run.

Remarks

The following table lists the available settings for intWindowStyle:

intWindowStyle | Description
1 | Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position.
3 | Activates the window and displays it as a maximized window.
7 | Minimizes the window and activates the next top-level window in the Z order.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.

See Also

Applies To: WshShortcut Object


WorkingDirectory Property

Provides the working directory of a shortcut object.

object.WorkingDirectory = strWorkingDirectory

Arguments

object

WshShortcut object.

strWorkingDirectory

Directory in which the shortcut will start.

Example

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each creates a shortcut to the script being run, and a URLShortcut to www.microsoft.com:

<package>

<job id="vbs">
<script language="VBScript">
   set WshShell = WScript.CreateObject("WScript.Shell")
   strDesktop = WshShell.SpecialFolders("Desktop")
   set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
   oShellLink.TargetPath = WScript.ScriptFullName
   oShellLink.WindowStyle = 1
   oShellLink.Hotkey = "CTRL+SHIFT+F"
   oShellLink.IconLocation = "notepad.exe, 0"
   oShellLink.Description = "Shortcut Script"
   oShellLink.WorkingDirectory = strDesktop
   oShellLink.Save

   set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
   oUrlLink.TargetPath = "http://www.microsoft.com"
   oUrlLink.Save
</script>
</job>

<job id="js">
<script language="Javascript">
   var WshShell = new ActiveXObject("WScript.Shell");
   strDesktop = WshShell.SpecialFolders("Desktop");
   var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
   oShellLink.TargetPath = WScript.ScriptFullName;
   oShellLink.WindowStyle = 1;
   oShellLink.Hotkey = "CTRL+SHIFT+F";
   oShellLink.IconLocation = "notepad.exe, 0";
   oShellLink.Description = "Shortcut Script";
   oShellLink.WorkingDirectory = strDesktop;
   oShellLink.Save();

   var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
   oUrlLink.TargetPath = "http://www.microsoft.com";
   oUrlLink.Save();
</script>
</job>

</package>

For more information, see Running Your Scripts.


WSH Version Information

The following table lists the version of Windows Script Host implemented by Microsoft host applications.

Host Application | WSH Version
 | 1.0 | 2.0
Microsoft Windows 98 | x |
Microsoft Windows NT 4 Option Pack | x |
Microsoft Windows 2000 |  | x

The following table lists Windows Script Host language elements and the version in which they were introduced.

Element | Version First Introduced
 | 1.0 | 2.0
<?job ?> Element |  | x
<?XML ?> Element |  | x
AddPrinterConnection Method | x |
AddWindowsPrinterConnection Method |  | x
AppActivate Method |  | x
Application Property | x |
Arguments Property | x |
AtEndOfLine Property |  | x
AtEndOfStream Property |  | x
Close Method |  | x
Column Property |  | x
ComputerName Property | x |
ConnectObject Method |  | x
Count Property | x |
CreateObject Method | x |
CreateShortcut Method | x |
Description Property | x |
DisconnectObject Method | x |
Echo Method | x |
EnumNetworkDrives Method | x |
EnumPrinterConnections Method | x |
Environment Property | x |
ExpandEnvironmentStrings Method | x |
FullName Property | x |
GetObject Method | x |
getResource Method |  | x
HotKey Property | x |
IconLocation Property | x |
Item Property | x |
<job> Element |  | x
Length Property | x |
Line Property |  | x
LogEvent Method |  | x
MapNetworkDrive Method | x |
Name Property | x |
<object> Element |  | x
<package> Element |  | x
Path Property | x |
Popup Method | x |
Quit Method | x |
Read Method |  | x
ReadAll Method |  | x
ReadLine Method |  | x
<reference> Element |  | x
RegDelete Method | x |
RegRead Method | x |
RegWrite Method | x |
Remove Method | x |
RemoveNetworkDrive Method | x |
RemovePrinterConnection Method | x |
<resource> Element |  | x
Run Method | x |
Save Method | x |
<script> Element |  | x
ScriptFullName Property | x |
ScriptName Property | x |
SendKeys Method |  | x
SetDefaultPrinter Method | x |
Skip Method |  | x
SkipLine Method |  | x
Sleep Method |  | x
SpecialFolders Property | x |
StdErr Property |  | x
StdIn Property |  | x
StdOut Property |  | x
TargetPath Property | x |
UserDomain Property | x |
UserName Property | x |
Version Property | x |
WindowStyle Property | x |
WorkingDirectory Property | x |
Write Method |  | x
WriteBlankLines Method |  | x
WriteLine Method |  | x
WScript Object | x |
WshArguments Object | x |
WshEnvironment Object | x |
WshNetwork Object | x |
WshShell Object | x |
WshShortcut Object | x |
WshSpecialFolders Object | x |
WshUrlShortcut Object | x |


Copyright

Microsoft® Windows® Script Host

Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, people and events depicted herein are fictitious and no association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

© 1991-2000 Microsoft Corporation. All rights reserved.

Microsoft, MS, MS-DOS, ActiveX, JScript, Microsoft Press, MSDN, Visual Basic, Windows, Windows NT, Win32, and Win32s are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.


SetDefaultPrinter Method

Sets the default printer to the remote printer specified.

object.SetDefaultPrinter strPrinterName

Arguments

object

WshNetwork object.

strPrinterName

Remote printer name to set as default, such as "\\Server\Printer1."

Example

The following example uses the AddPrinterConnection method to connect a network printer to LPT1: and then sets it as the default printer:

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.AddPrinterConnection "LPT1:", "\\Server\Print1"
WshNetwork.SetDefaultPrinter "\\Server\Print1"

See Also

AddPrinterConnection Method | AddWindowsPrinterConnection Method | EnumPrinterConnections Method | RemovePrinterConnection Method

Applies To: WshNetwork Object


Microsoft Windows Script Host Reference

Windows Script Host

Windows Script Host Language Reference

Alphabetic Keyword List

Elements

Errors

Methods

Objects

Properties

Welcome to the Windows Script Host Reference

This information will help you explore the many different parts of Windows Script Host.

Each part of the Windows Script Host language is listed alphabetically under the Alphabetic Keyword List. If you want to examine just one category, say, objects, just click on the objects heading, and you will only see Windows Script Host objects.

Click on a heading to the left to display a list of items contained in that category. From this list, select the topic that you want to view. Once you have opened a topic, you can easily link to other related sections.

Feel free to navigate through the various parts that make up Windows Script Host Language Reference, and see just how versatile the Windows Script Host language can be.

Language Element | Description
<?job ?> Element | XML processing instruction that specifies attributes for error handling.
<?XML ?> Element | Indicates that the file should be parsed as XML.
AddPrinterConnection Method | Maps the remote printer specified by strRemoteName to the local resource name strLocalName.
AddWindowsPrinterConnection Method | Adds a printer connection to Windows.
AppActivate Method | Activates an application window.
Application Property | Provides the IDispatch interface on the WScript object.
Arguments Property | Returns a pointer to the WshArguments collection or identifies arguments for the shortcut.
AtEndOfLine Property | Returns True if the stream pointer immediately precedes the end-of-line marker in an input stream; False if it is not.
AtEndOfStream Property | Returns True if the stream pointer is at the end of an input stream; False if it is not.
Close Method | Closes an open stream.
Column Property | Returns the column number of the current character position in an input stream.
ComputerName Property | Provides a string representation of the computer name.
ConnectObject Method | Connects an objects event sources to functions with a given prefix.
Count Property | Returns the number of enumerated items.
CreateObject Method | Creates an object specified by the strProgID parameter.
CreateShortcut Method | Creates an object reference to a shortcut or URLshortcut.
Description Property | Provides a description of a shortcut object.
DisconnectObject Method | Disconnects a previously connected object from Windows Script Host.
Echo Method | Displays parameters in a window.
EnumNetworkDrives Method | Returns the current network drive mappings.
EnumPrinterConnections Method | Returns the current network printer mappings.
Environment Property | Returns the WshEnvironment object.
Errors | Lists Windows Script Host error messages.
ExpandEnvironmentStrings Method | Expands the PROCESS environment variable and returns the result string.
FullName Property | Returns a string containing the full path to the host executable or shortcut object.
GetObject Method | Retrieves an Automation object from a file, or an object specified by the strProgID parameter.
getResource Method | Gets the value of a resource defined with the <resource> element.
HotKey Property | Provides the hotkey of a shortcut object.
IconLocation Property | Provides the icon location of a shortcut object.
Item Property | Returns a specified item from a collection.
<job> Element | Allows for multiple jobs to be defined in a single file. Since this is an enclosing tag, every tag within the Job tag will apply to that job.
Length Property | Returns the number of enumerated items.
Line Property | Returns the current line number in an input stream.
LogEvent Method | Logs an event in the Windows NT event log or WSH.log file.
MapNetworkDrive Method | Maps the share point specified by strRemoteName to the local resource name strLocalName.
Name Property | Provides a string containing the friendly name of the WScript object.
<object> Element | XML element used in scriptlet files that defines objects that can be referenced by script.
<package> Element | Encloses multiple job definitions.
Path Property | Provides a string containing the name of the directory where WScript.Exe or Cscript.Exe resides.
Popup Method | Displays a popup message box window that contains the message contained in strText.
Quit Method | Quits execution with a specified error code.
Read Method | Reads a specified number of characters from an input stream and returns the resulting string.
ReadAll Method | Reads an entire input stream and returns the resulting string.
ReadLine Method | Reads an entire line (up to, but not including, the newline character) from an input stream and returns the resulting string.
<reference> Element | XML element used in Windows Script Component files that includes a reference to an external type library.
RegDelete Method | Deletes from the registry the key or value named strName.
RegRead Method | Returns the registry key or value named by strName.
RegWrite Method | Sets the registry key or value named by strName.
Remove Method | Deletes the environment variable specified by strName.
RemoveNetworkDrive Method | Removes the current resource connection denoted by strName.
RemovePrinterConnection Method | Removes the current resource connection denoted by strName.
<resource> Element | XML element isolates textual or numeric data that should not be hard-coded into the scripts.
Run Method | Creates a new process that executes strCommand.
Save Method | Saves a shortcut to the specified location.
<script> Element | XML element that contains script to define the Windows Script Component's behavior.
ScriptFullName Property | Provides the full path to the script being run.
ScriptName Property | Provides the file name of the script being run.
SendKeys Method | Sends one or more keystrokes to the active window as if typed at the keyboard.
SetDefaultPrinter Method | Sets the default printer to the remote printer specified.
Skip Method | Skips a specified number of characters when reading an input stream.
SkipLineMethod | Skips the next line when reading an input stream.
Sleep Method | Places the script process into an inactive state for the number of milliseconds specified and then continues execution.
SpecialFolders Property | Provides the WshSpecialFolders object for accessing Windows shell folders such as the desktop folder, the Start menu folder, and the personal document folder.
StdErr Property | Exposes the write-only error output stream for the current script.
StdIn Property | Exposes the read-only input stream for the current script.
StdOut Property | Exposes the write-only output stream for the current script.
TargetPath Property | Sets the path of the file to execute of a shortcut object.
UserDomain Property | Provides a string representation of the user domain name.
UserName Property | Provides a string representation of the user name.
Version Property | Returns the version of the Windows Script Host.
WindowStyle Property | Provides the window style of a shortcut object.
WorkingDirectory Property | Provides the working directory of a shortcut object.
Write Method | Writes a specified string to an output stream.
WriteBlankLines Method | Writes a specified number of newline characters to an output stream.
WriteLine Method | Writes a specified string and newline character to an output stream.
WScript Object | Exposes properties that tell the path of the running scripting host (wscript.exe or cscript.exe), its arguments, and the working mode (interactive or batch). The WScript object also provides methods to create and read objects.
WshArguments Object | Returns a pointer to the collection of command-line parameters.
WshEnvironment Object | Retrieves the system environment variables.
WshNetwork Object | Maps the network, making it easy to connect and disconnect remote drives and printers.
WshShell Object | Starts a new process and creates shortcuts, and provides the Environment collection to handle environmental variables such as WINDIR, PATH, or PROMPT.
WshShortcut Object | Creates an object reference to a shortcut.
WshSpecialFolders Object | Accesses the Windows shell folders such as the desktop folder, the Start menu folder, and the personal document folder.
WshUrlShortcut Object | Creates an object reference to URLshortcut.
Language Element | Description
<?job ?> Element | XML processing instruction that specifies attributes for error handling.
<?XML ?> Element | Indicates that the file should be parsed as XML.
<job> Element | Allows for multiple jobs to be defined in a single file. Since this is an enclosing tag, every tag within the Job tag will apply to that job.
<object> Element | XML element used in Windows Script Component files that defines objects that can be referenced by script.
<package> Element | Encloses multiple job definitions.
<reference> Element | XML element used in Windows Script Component files that includes a reference to an external type library.
<resource> Element | XML element isolates textual or numeric data that should not be hard-coded into the scripts.
<script> Element | XML element that contains script to define the Windows Script Component's behavior.
Language Element | Description
Error List | List of Windows Script Host Error messages.
Language Element | Description
AddPrinterConnection Method | Maps the remote printer specified by strRemoteName to the local resource name strLocalName.
AddWindowsPrinterConnection Method | Adds a printer connection to Windows.
AppActivate Method | Activates an application window.
Close Method | Closes an open stream.
ConnectObject Method | Connects an objects event sources to functions with a given prefix.
CreateObject Method | Creates an object specified by the strProgID parameter.
CreateShortcut Method | Creates an object reference to a shortcut or URLshortcut.
DisconnectObject Method | Disconnects a previously connected object from Windows Script Host.
Echo Method | Displays parameters in a window.
EnumNetworkDrives Method | Returns the current network drive mappings.
EnumPrinterConnections Method | Returns the current network printer mappings.
ExpandEnvironmentStrings Method | Expands the PROCESS environment variable and returns the result string.
GetObject Method | Retrieves an Automation object from a file, or an object specified by the strProgID parameter.
getResource Method | Gets the value of a resource defined with the <resource> element.
LogEvent Method | Logs an event in the Windows NT event log or WSH.log file.
MapNetworkDrive Method | Maps the share point specified by strRemoteName to the local resource name strLocalName.
Popup Method | Displays a popup message box window that contains the message contained in strText.
Quit Method | Quits execution with a specified error code.
Read Method | Reads a specified number of characters from an input stream and returns the resulting string.
ReadAll Method | Reads an entire input stream and returns the resulting string.
ReadLine Method | Reads an entire line (up to, but not including, the newline character) from an input stream and returns the resulting string.
RegDelete Method | Deletes from the registry the key or value named strName.
RegRead Method | Returns the registry key or value named by strName.
RegWrite Method | Sets the registry key or value named by strName.
Remove Method | Deletes the environment variable specified by strName.
RemoveNetworkDrive Method | Removes the current resource connection denoted by strName.
RemovePrinterConnection Method | Removes the current resource connection denoted by strName.
Run Method | Creates a new process that executes strCommand.
Save Method | Saves a shortcut to the specified location.
SendKeys Method | Sends one or more keystrokes to the active window as if typed at the keyboard.
SetDefaultPrinter Method | Sets the default printer to the remote printer specified.
Skip Method | Skips a specified number of characters when reading an input stream.
SkipLineMethod | Skips the next line when reading an input stream.
Sleep Method | Places the script process into an inactive state for the number of milliseconds specified and then continues execution.
Write Method | Writes a specified string to an output stream.
WriteBlankLines Method | Writes a specified number of newline characters to an output stream.
WriteLine Method | Writes a specified string and newline character to an output stream.
Language Element | Description
WScript Object | Exposes properties that tell the path of the running scripting host (wscript.exe or cscript.exe), its arguments, and the working mode (interactive or batch). The WScript object also provides methods to create and read objects.
WshArguments Object | Returns a pointer to the collection of command-line parameters.
WshEnvironment Object | Retrieves the system environment variables.
WshNetwork Object | Maps the network, making it easy to connect and disconnect remote drives and printers.
WshShell Object | Starts a new process and creates shortcuts, and provides the Environment collection to handle environmental variables such as WINDIR, PATH, or PROMPT.
WshShortcut Object | Creates an object reference to a shortcut.
WshSpecialFolders Object | Accesses the Windows shell folders such as the desktop folder, the Start menu folder, and the personal document folder.
WshUrlShortcut Object | Creates an object reference to URLshortcut.
Language Element | Description
Application Property | Provides the IDispatch interface on the WScript object.
Arguments Property | Returns a pointer to the WshArguments collection or identifies arguments for the shortcut.
AtEndOfLine Property | Returns True if the stream pointer immediately precedes the end-of-line marker in an input stream; False if it is not.
AtEndOfStream Property | Returns True if the stream pointer is at the end of an input stream; False if it is not.
Column Property | Returns the column number of the current character position in an input stream.
ComputerName Property | Provides a string representation of the computer name.
Count Property | Returns the number of enumerated items.
Description Property | Provides a description of a shortcut object.
Environment Property | Returns the WshEnvironment object.
FullName Property | Returns a string containing the full path to the host executable or shortcut object.
HotKey Property | Provides the hotkey of a shortcut object.
IconLocation Property | Provides the icon location of a shortcut object.
Item Property | Returns a specified item from a collection.
Length Property | Returns the number of enumerated items.
Line Property | Returns the current line number in an input stream.
Name Property | Provides a string containing the friendly name of the WScript object.
Path Property | Provides a string containing the name of the directory where WScript.Exe or CScript.Exe resides.
ScriptFullName Property | Provides the full path to the script being run.
ScriptName Property | Provides the file name of the script being run.
SpecialFolders Property | Provides the WshSpecialFolders object for accessing Windows shell folders such as the desktop folder, the Start menu folder, and the personal document folder.
StdErr Property | Exposes the write-only error output stream for the current script.
StdIn Property | Exposes the read-only input stream for the current script.
StdOut Property | Exposes the write-only output stream for the current script.
TargetPath Property | Sets the path of the file to execute of a shortcut object.
UserDomain Property | Provides a string representation of the user domain name.
UserName Property | Provides a string representation of the user name.
Version Property | Returns the version of the Windows Script Host.
WindowStyle Property | Provides the window style of a shortcut object.
WorkingDirectory Property | Provides the working directory of a shortcut object.

© 2000 Microsoft Corporation. All rights reserved.


Microsoft Windows Script Host Reference

Microsoft® Windows® Script Host
Windows Script Host Reference

Alphabetic
Keyword List
Elements

Errors

Methods

Objects

Properties

© 2000 Microsoft Corporation. All rights reserved.