Today I’m sharing a script I wrote that helps Windows Administrators manage users’ printer queues. If you haven’t already, please check out my post PowerShell Script to Audit Printer Queue Usage post. This little baby will take a look at a Printer Server’s event log and give you a CSV file showing which printer queues are actually being utilized by users. It may help you to make some decisions when it comes to your printer queues.

The following script is intended to be run as a Group Policy Logon script. It can, of course, be run manually. The script generates output which I like to dump a text file to the users’ home drive in order to determine if the script performed as expected. In order to do this from Group Policy it will need to be accompanied by a batch file to launch it.

REM AddRemovePrinters.cmd
cscript "\\network_path\to\AddRemovePrinters.vbs" > %HOMEDRIVE%%HOMEPATH%AddRemovePrinters.log 2>&1

AddRemovePrinters.vbs can do the following functions.

  • REMOVE a Printer
  • Remove an old queue and REPLACE it with a new queue. (Could be used to help migrate off an old print server)
  • Remove a specified queue, and RE-ADD the same queue. (Can help fix some issues when updating a queue printer driver.)

Check out the IsTargetedPrinter() function in the script to see how to configure it. The configuration of the script is done in this function.

Note: If you configure a printer queue to be removed and re-added, the user will loose their custom settings on the printer every time the script is run. I use the remove/readd functionality when I’m updating printer drivers. It seems to help the client workstation update the drivers.

'       Script: AddRemovePrinters.vbs
'       Author: Gregory Strike
'         URL: //
'         Date: 2010-08-19
' Last Updated: 2011-04-04
'  Description: This script helps manage printers in a users' profile.
'               Printer queues to be managed are defined under the
'               IsTargetedPrinter() function.
'         Note: This is designed to be run as a Group Policy Logon script.

Dim objNetwork, objPrinter, intNetLetter, X
Dim Printer, NumRemovePrinters
Dim AddPrinters()
Redim AddPrinters(1)
Dim RemovePrinters()
Redim RemovePrinters(1)
NumRemovePrinters = 0 

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

Function IsTargetedPrinter(PassedPrinter)
	'Remove and Readd the Printer, Return the SAME name.
	'Replaced the printer, Return the NEW printer name.
	'Delete a printer, Return "DELETE".

	AddPrinter = ""
	myPrinter = UCase(PassedPrinter)

	'In this example \\PRNTSRV\Printer1 will be removed and re-added.
	If myPrinter = "\\PRNTSRV\Printer1" Then AddPrinter = "\\PRNTSRV\Printer1"

	'In this example \\PRNTSRV\Printer1 will be removed and \\NEW-PRNTSRV\Printer1 will be added.
	If myPrinter = "\\PRNTSRV\Printer1" Then AddPrinter = "\\NEW-PRNTSRV\Printer1"

	'In this example \\PRNTSRV\Printer2 will be deleted.
	If myPrinter = "\\PRNTSRV\Printer2" Then AddPrinter = "DELETE"

	IsTargetedPrinter = AddPrinter
End Function

Function RemoveThisPrinter(Printer)
	NumRemovePrinters = NumRemovePrinters + 1
	If NumRemovePrinters = 0 Then
		RemovePrinters(0) = Printer
		Redim Preserve RemovePrinters(NumRemovePrinters - 1)
		RemovePrinters(NumRemovePrinters - 1) = Printer
	End If
End Function

Function AddThisPrinter(Printer)
	NumAddPrinters = NumAddPrinters + 1
	If NumAddPrinters = 0 Then
		AddPrinters(0) = Printer
		Redim Preserve AddPrinters(NumAddPrinters - 1)
		AddPrinters(NumAddPrinters - 1) = Printer
	End If
End Function

'See if there are any printers
Set objPrinter = objNetwork.EnumPrinterConnections

If objPrinter.Count = 0 Then
	WScript.Echo "No printers found.  Printers may have still been creating when script ran."
End If

WScript.Echo ""
WScript.Echo "Finding Targeted Network Printers..."
For X = 0 To (objPrinter.Count - 1) Step 2
	Printer = objPrinter.Item(X + 1)
	TargetedPrinter = IsTargetedPrinter(Printer)
	If TargetedPrinter <> "" Then
		If Printer = TargetedPrinter Then
		End If
		If Printer <> TargetedPrinter Then
			If TargetedPrinter <> "DELETE" Then
			End If
		End If
		WScript.Echo Printer & " --> " & TargetedPrinter
	End If

'See if there are any targeted printers
If NumRemovePrinters=0 Then
	WScript.Echo "No printers found."
End If

WScript.Echo ""
WScript.Echo "Finding Default Printer..."
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer where Default = 'True'")
For Each objDefaultPrinter in colInstalledPrinters
	If IsTargetedPrinter(objDefaultPrinter.Name) <> "" Then
		DefaultPrinter = IsTargetedPrinter(objDefaultPrinter.Name)
		DefaultPrinter = objDefaultPrinter.Name
	End If
	Wscript.Echo DefaultPrinter

WScript.Echo ""
WScript.Echo "Removing Printer Connections..."
For X = 0 To Ubound(RemovePrinters)
	If RemovePrinters(X) <> "" Then
		WScript.Echo RemovePrinters(X)
		objNetwork.RemovePrinterConnection RemovePrinters(X)
	End If

WScript.Sleep 10

WScript.Echo ""
WScript.Echo "Adding Printer Connections..."
For X = 0 To Ubound(AddPrinters)
	If AddPrinters(X) <> "" Then
		WScript.Echo AddPrinters(X)
		objNetwork.AddWindowsPrinterConnection AddPrinters(X)
	End If

WScript.Echo ""
WScript.Echo "Setting Default Printer..."
If DefaultPrinter = "DELETE" Then
	WScript.Echo "Script is configured to remove this users default printer.  No action taken."
	WScript.Echo DefaultPrinter
	objNetwork.SetDefaultPrinter DefaultPrinter
End If


Gregory Strike

Husband, father, IT dude & blogger wrapped up into one good looking package.