So we had this old Windows printer server that needed to be replaced. The server had about 200 printer queues shared to about a thousand computers. A new printer server was set up and new printer shares were made. At the same time we decided to deploy a new printer naming policy, so the printer queues got new names on the new server. Now the only problem was to replace the printer queue connections on the 1000+ workstations from the old server to the corresponding queues on the new server. As our users haven't got that good computing skills we realized we couldn't just post instructions to the users on how to replace the old queues with the new ones. After some thinking I came up with the idea to use a script to replace the old queues with the new ones on the workstations.



The script works as follows:
First you need to make a file containing a list of old queues and their corresponding new queues. Let the file be named filename.txt in this example. Here's what filename.txt could look like:

\\OldServer\OldPrinterQueueName,\\NewServer\NewPrinterQueueName
\\OldServer\AnotherOldQueue,\\NewServer\AnotherNewQueue
\\AnotherOldServer\AThirdQueue,\\AnotherNewServer\AThirdQueue

As you probably can see, each line comprises an old queue and a new queue, separated by a comma. That's all. You may make the list as long as you want as long as each line contains just this comma-separated pair of printer queues. No comments, no other text is allowed, just the comma-separated pairs of queues, one pair per line.

Then you need to edit the script and insert the full UNC path of the file you just created. If you named the file "filename.txt", saved it on a share called "ShareName" on a server, named "Server", then you would enter "\\Server\ShareName\FileName.txt" as the value of the strFile variable in the script.

Finally you should make sure this script runs for each user in your environment. This can be done e.g. by calling it from the logon script. As the script might take some time to run you might want to make sure it runs only once for each user. This, in turn, may be accomplished e.g. by adding a few lines in your users' logon script(s), checking if a certain file exists in the user's profile. If the file exists, the logon script will skip calling the printer change script. If the file doesn't exist the logon script should call the printer change script and create this certain file in the user's profile to avoid that the printer change script is run the next time the user logs on.

Ok, that's probably all you need to know about the script. Here's the script code. You may use it free of charge as long as I'm given the appropriate credit. You may also change it as you wish, but you may not remove my name from it.


' Script that changes the user's printer(s) to other(s) printer(s) according to a csv list of printers.
' The list of printers should contain comma separated printer pairs, one pair at each line, like this:
' \\server1\printer1,\\server2\printer2
' where \\server1\printer1 is the printer that shall be replaced,
' and \\server2\printer2 is the printer that shall replace the beforementioned printer.
' The script can be called from a e.g. the logon script and the script can replace several different printers.
' Just add more lines to the list of printers and the script will go through them all.
' The script also notes which printer is the default printer and sets the replacing printer as default printer.
' Created by Dennis Nyholm 23.7.2008

'Fill in the filename of the list of printers here:
set strFile = "\\servername\sharename\filename.txt"

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFile, ForReading)
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.Readline
arrprt = Split(strLine, ",")
Sourceprt = arrprt(0)
Destprt = arrprt(1)
'Wscript.Echo Sourceprt & " => " & Destprt
strComputer = "."
'Set error handling on:
'On Error Resume Next
Set objNetwork = CreateObject("Wscript.Network")

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")
For Each objPrinter in colInstalledPrinters
'Wscript.Echo "Printer Name: " & objPrinter.Name
If LCase(objPrinter.Name) = LCase(Sourceprt) Then
Wscript.Echo objPrinter.Name & " will be replaced by " & Destprt
'Install replacing printer:
objNetwork.AddWindowsPrinterConnection Destprt

'Check default printer:
Set WshShell = WScript.CreateObject("WScript.Shell")
strDefaultPrinter = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
arrDefaultPrinter = Split(strDefaultPrinter, ",")

'Check if this is the default printer:
If LCase(arrDefaultPrinter(0)) = LCase(objPrinter.Name) Then
'Wscript.Echo "Is default printer"
'Set the printer as default:
objNetwork.SetDefaultPrinter Destprt
End If

'Remove old printer:
objNetwork.RemovePrinterConnection Sourceprt
End If

Next
Loop