Windows Logon Scripts – EnumPrinterConnections

Introduction to EnumPrinterConnections

EnumPrinterConnections is not a command to use in isolation.  Before you try this  EnumPrinterConnections method, I suggest that you create a VBScript with AddWindowsPrinterConnection.  My reasoning is that you need a printer or two before you start counting them.

Topics for EnumPrinterConnections


Enumerate Mapped Network Drives Scenario

EnumPrinterConnections reminds me of a door handle, both are useless until they are attached to an object. The EnumPrinterConnections method opens the way for the AddWindowsPrinterConnection or SetDefaultPrinter VBScript commands.

Let us suppose that you want to script extra ‘If …then.. else’ logic.  Without EnumPrinterConnections, it would be difficult to get a handle on the printers, but with EnumPrinterConnections, it’s easy for the script to list the printers that are already in connected.  Once you enumerate the printers, it is but a short step to manipulate them,  for example, you can use logic such as, if strUNCPrinter  = HP then do nothing, else if strUNCPrinter  = Epson, then set as default printer.

EnumPrinterConnections Syntax

Most VBScript commands are singular, but the Enum family have a plural ending.  There is a good reason for the ‘s’ at the end, it is because Enum commands represent an array, or collection.  The syntax however, is deceptively simple for example:

objDrive = objNetwork.EnumPrinterConnections

When it comes to output, which actually displays the Enumerated printers, then we need extra VBScript commands.  Therefore, it is no good just enumerating the printers, we want the script to methodically cycle through and display their names and the LPT ports, here is the code.

For intDrive = 0 to objDrive.Count -1 Step 2
intNetLetter = IntNetLetter +1
WScript.Echo "UNC Path " & objDrive.Item(intDrive) & " = " _
& objDrive.Item(intDrive +1) & " Mapped drive No : " & intDrive

Technically, EnumPrinterConnections outputs a collection of paired items.  The even numbers in each pair are the printer ports and the odd numbers represent the UNC paths.  Think of the collection as an array.  Imagine the printer port in one column and the UNC path in the other.  You could try altering the values for Step, but I find that 2 is the best number.  Step 2 makes sense, as after all these are paired items.

Example 1a Preparation

EnumPrinterConnections is complex to code, this is because it needs other VBScript commands. To give the script the best chance of working, we need to create one or more printers, otherwise there would be nothing for the script to enumerate.


  1. The reason that we are creating extra printers here is so that we can study EnumPrinterConnections.
  2. On Line 10, change the server name from ‘\\LittleServer’ to the name of your server.
  3. Make sure that change the printer UNC to refer to an actual printer on your network.

Instructions for AddWindowsPrinterConnection

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g. EnumPrinters.vbs.
  3. Double click and check in your Printers and faxes folder.

‘ Printers.vbs – Windows Logon Script.
‘ VBScript – to map a network printer
‘ Author Guy Thomas
‘ Version 1.3 – April 24th 20
Option Explicit
Dim objNetwork, strUNCPrinter
strUNCPrinter = "\\LittleServer\HP LaserJet 4L"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter "HP LaserJet 4L"
WScript.Echo "Check the Printers folder for : " & strUNCPrinter


‘ End of Guy’s Windows logon script.

Guy Recommends: Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try – it’s free!

Download Permissions Analyser – Free Active Directory Tool

Example 1b the Main EnumPrinterConnections Script

Instructions to EnumPrinterConnections

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g. EnumPrinters.vbs.
  3. Double click the script and launch your Printers and faxes folder.

‘ EnumPrinters.vbs N.B. This script need printers
‘ VBScript to Enumerate Printers.
‘ Author Guy Thomas
‘ Version 1.8 – April 24th 2010
‘ ———————————————————–‘
Option Explicit
Dim objNetwork, objPrinter, intDrive, intNetLetter

‘ This is the heart of the script
‘ Here is where objPrinter enumerates the mapped drives
Set objNetwork = CreateObject("WScript.Network")
Set objPrinter = objNetwork.EnumPrinterConnections

‘ Extra section to troubleshoot
If objPrinter.Count = 0 Then
WScript.Echo "Guy’s warning: No Printers Mapped "
End If

‘ Here is the where the script reads the array
For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter +1
WScript.Echo "UNC Path " & objPrinter.Item(intDrive) & " = " _
& objPrinter.Item(intDrive +1) & " Printer : " & intDrive


‘ End of Guy’s Windows Logon Script<.p>

Learning Points

Note 1: The If… then else section around Lines 14 -18 is not strictly necessary, however I included this code to alert people who do not have any mapped network drives.

Note 2: This method has two separate functions.  Firstly, the EnumPrinterConnections method itself, which makes the mapped printers ‘visible’.  Secondly, the objPrinter.Count (Line 21-25), which is able to read the printer names and port numbers because they have been enumerated on line 12.

Note 3: To me the most puzzling part of the script is .count -1.  My latest theory is that this command tells the script to walk through the list of printers starting with the lowest number.  As ever, practical tests are the best, so, try objPrinter.count 1 or any positive number, it does not work,  However, if you try -3 EnumPrinterConnections skips the first mapped pair of printer references.

N.B. Extra information supplied by Mike Saunders.

The reason you have to use .Count – 1 is because
Set objPrinter = objNetwork.EnumPrinterConnections initializes objPrinter as an array. Actually, I don’t think it’s an array, it’s more like a list. If you have two printers on your computer, this is what would happen. Let’s say I have these two printers: \\myserver\hplj1320 \\myserver\hplj4100

objNetwork.EnumPrinterConnections would give objPrinter 4 items.
"", "\\myserver\hplj1320", "", "\\myserver\hplj4100". This list is indexed starting from 0. Another way it could be represented would be:

objPrinter(0) = ""
objPrinter(1) = "\\myserver\hplj1320"
objPrinter(2) = ""
objPrinter(3) = "\\myserver\hplj4100"

The reason for the .Count – 1 becomes apparent when you look at the list and the code:
For intDrive = 0 to objPrinter.Count -1 Step 2

The .Count method would return 4, as there are 4 items in the list. If you are starting at 0, there are five numbers between 0 and 4: 0,1,2,3, and 4. If you tried to use objPrinter.Item(intDrive) where intDrive = 4, you’d get an out of bounds error.

The reason for Step = 2 is because you are only concerned with the UNC path of the printer, which is the second object of each tuple, in this case the even numbered items in the list.

So, there you have it. MS.

Note 4: Step 2 is the best value.  Step with any odd number produces a subscript out of range error, while step 4, takes giant paces and misses every other printer.

Guy Recommends SolarWinds’ Free Network MonitorSolarwinds Network Device Monitor

Thus utility makes it easy to check the health of a router or firewall.  Check the real-time performance, and availability statistics, for any device on your network.  Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl – it’s free. Download your free Network Device Monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

Use PowerShell for Logon Scripts

 VBScripts are being superseded by PowerShell .ps1 files.  While PowerShell is used mainly for configuring the operating system, it’s possible to use PowerShell’s cmdlets to work with methods such as EnumPrinterConnections. The technique is to create a ComObject, which can act as a wrapper for familiar VBScript commands. Here is example of PowerShell’s New-Object cmdlet working with EnumPrinterConnections:

# PowerShell Logon 3 Script Example
$Net = $(New-Object -ComObject WScript.Network)

You could save these instructions in a .ps1 file.  However, the hard part is executing  this .ps1 file as a logon script.  See more about assigning PowerShell logon scripts.

EnumPrinterConnections Summary

EnumPrinterConnections is a difficult technique.  The best approach is to master first master the AddWindowsPrinterConnection method first then graduate to this counting and displaying method.  An added bonus of starting with AddWindowsPrinterConnection is that you will create an actual printer to enumerate.  Pay careful attention to the syntax; from the spelling with the plural drives, to understanding the two parts, enumerating the printers, then displaying the array of LPT ports and UNC path.

Once you have mastered EnumPrinterConnections, then you can play ‘If… then … else’ games with your logon scripts. For example, if drive a printer is already connected, then remove it and then map a different printer.


Download my Logon Script eBook for only $6.25

Logon ScriptThe extra features you get in your eBook include, more pages full of detailed examples.  Also, ten ‘how to…’ sections, with screen shots showing which menus to use.  Go for Guy’s eBook – and get a printable version with copy enabled and no expiry date.

  Jumbo Script 7 Package

See more printer logon scripts examples

Logon Script Home   • Printer computer   • Remove printer logon script

AddWindowsPrinterConnection script   • Multiple printers    • Assign Logon Script AD

• EnumPrinterConnections   • SetDefaultPrinter script   • Free Import CSV Tool