WshNetwork Object

Microsoft® Windows® 2000 Scripting Guide

The WshNetwork object provides your scripts with the ability to work with network drives and printers. It also provides your scripts with access to the name of the computer they are currently running on, as well as the domain and user names of the account under which they are running. This makes WshNetwork extremely useful in scripts that take actions based on such information as the name of the user logging on or the domain in which the user account resides.

The WshNetwork object can be used to accomplish some of the same tasks as the net.exe command-line tool. For example, the net name command returns the name of the user and the computer; the net use command is used to map and unmap network drives. These similarities make WshNetwork useful for converting legacy batch files and logon scripts that use net.exe to a WSH-based solution.

Table 3.21 lists the capabilities provided by the WshNetwork object, along with the methods and properties that your scripts can use to access this functionality.

Table 3.21 Capabilities Provided by the WshNetwork Object


Method or Property

Working with network drives




Working with network printers






Obtaining information about the currently logged-on user




WshNetwork is part of the Windows Script Host Object Model, wshom.ocx. The object model for the WshNetwork object is shown in Figure 3.15. In addition to the methods and properties provided by the WshNetwork object, WshNetwork also exposes a WshCollection object. The WshCollection object is not created using CreateObject but is automatically created and returned by the WshNetwork objects EnumNetworkDrives and EnumPrinterConnections methods.

Figure 3.15 shows the properties and methods of the WshNetwork object.

Figure 3.15 WshNetwork Object Model


Accessing the WshNetwork Object

The WshNetwork object is a COM object, and an instance of the object can be created by the following code statement:

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