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!

Comments

  • Anonymous
    January 01, 2003
    Hi Jeff, Thank you for your explanation of the Wscript.Network object.   My webpage now obtains UserName via JavaScript -> VBScript -> Wscript.Network, with your help.   Thanks again.   Regards, Dave

  • Anonymous
    February 23, 2018
    Thank you for the post! It's very useful!Maybe it's too late to ask this, but I'll take the risk! :)Is there a way to get the user being used in a concrete mapped drive? "UserName" property seems to always return Windows logon user, rather than the user I specified when mapping to a network drive. I explain myself:Suppose I logon into windows with "Domain\User1". I connect a network drive WITH A DIFFERENT USER, say "Domain\User2": obj_Wsh.MapNetworkDrive "X:", "\server\resource", , "Domain\User2", "password_of_User2"Is there a way to programmatically get that "User2" from the connection to drive "X:"? In such scenario, UserName property always returns "User1"!Thanks in advance,Jose

  • Anonymous
    March 09, 2018
    How to map a network drive with wildcard character in VB Script to auto mapped with available drive