Wscript.Network

One of my favorite objects is the Wscript.Network object. It's a very simple way to access certain environment variables in a VBScript.

To invoke this object, this line must be inserted near the top of your script (certainly before you reference properties of this object):

 Set WshNetwork = CreateObject("Wscript.Network")

What you can do with this is query the properties of username, computername and userdomain. You can also do things like add a network drive and add a printer (as well as deleting them). This is great for a much more flexible login script processing than standard batch.

For example, if you want to map a drive:

 WshNetwork.MapNetworkDrive "x:" \\SERVERNAME\SHARE

maps a drive, and the commands to add a printer and make it the default are:

 WshNetwork.AddWindowsPrinterConnection \\printserv\DefaultPrinter
WshNetwork.SetDefaultPrinter \\printserv\DefaultPrinter

These code snippets apply to Windows 2000 and higher - other parameters may be necessary for earlier systems.

A complete technical description of this object can be found here: https://msdn2.microsoft.com/en-us/library/s6wt333f(vs.85).aspx, but here is a listing of the properties and methods:

Properties

  • UserName - this is obviously the username variable, likely the SAM Account Name (the pre-Windows 2000 name)
  • UserDomain - this is the NetBIOS domain name
  • ComputerName - this is the NetBIOS computer name

Methods

  • AddWindowsPrinterConnection - this is the command to add a printer in Windows 2000 and higher
  • AddPrinterConnection - this is the command to add a printer in older systems
  • EnumNetworkDrives - this command lists the mapped drives on the computer
  • EnumPrinterConnections - this command lists the installed printers
  • MapNetworkDrive - this command maps a network drive
  • RemoveNetworkDrive - this command removes a network drive
  • RemovePrinterConnection - this command removes a printer connection
  • SetDefaultPrinter - this command sets the default printer

I hope this is helpful for you. Let me know!