Adding and Removing Printers with VBScript

Here’s some VBScript code to add and remove printers, although it sounds like we are finally going to start using Group Policy Preferences to do the heavy lifting for printer deployment.  One thing I found is that this script doesn’t work too well on Windows 7.  I haven’t investigated it too deeply, but I believe it is due to the Point and Print group policy.  Disabling this policy allows the script to work.

Note that you should use UCASE, which will convert all the letters in the print queue to upper case letters before you do the comparison.  On some computers: the print queue would be all in upper case and some all in lower case, so using UCASE forces the strings to be compared in the same format.

Using WMI, it will also preserve the status of the default printer and adjust the default status accordingly.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer where Default = True")
For Each objPrinter in colInstalledPrinters
PrinterDefault=objPrinter.Name
Next


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

' Add COPIER_POD_07
WshNetwork.AddWindowsPrinterConnection "\ACMEDCCOPIER_POD_07"

' Add COPIER_POD_19
WshNetwork.AddWindowsPrinterConnection "\ACMEDCCOPIER_POD_19"

' Add PRINTER_POD_03
WshNetwork.AddWindowsPrinterConnection "\ACMEDCPRINTER_POD_03"

' Add PRINTER_POD_17
WshNetwork.AddWindowsPrinterConnection "\ACMEDCPRINTER_POD_17"

Set WSHPrinters = WSHNetwork.EnumPrinterConnections
PrinterPath = "\ACMEDCWK_ENG_PLTR_01"
PrinterExists = False
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If UCase(WSHPrinters.Item(LOOP_COUNTER +1)) = PrinterPath Then
PrinterExists = True
End If
Next
If PrinterExists Then
WshNetwork.RemovePrinterConnection "\ACMEDCWK_ENG_PLTR_01"
WshNetwork.AddWindowsPrinterConnection "\ACMEDCPLOTTER_POD_14"
If PrinterPath=PrinterDefault Then
WSHNetwork.SetDefaultPrinter "\ACMEDCPLOTTER_POD_14"
End If
End If

Set WSHPrinters = WSHNetwork.EnumPrinterConnections
PrinterPath = "\ACMEDCWK_ENG_LSR_01"
PrinterExists = False
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If UCase(WSHPrinters.Item(LOOP_COUNTER +1)) = PrinterPath Then
PrinterExists = True
End If
Next

If PrinterExists Then
WshNetwork.RemovePrinterConnection "\ACMEDCWK_ENG_LSR_01"
WshNetwork.AddWindowsPrinterConnection "\ACMEDCPRINTER_POD_14"
If PrinterPath=PrinterDefault Then
WSHNetwork.SetDefaultPrinter "\ACMEDCPRINTER_POD_14"
End If
End If

– Soli Deo Gloria