Move Users to a New Print Server [Script]

Below is a script I wrote a few months ago, it is especially useful if you are moving or changing the name of your printer server. The script looks at the current user’s installed network printers which are retrieved for example as.

\\printserver1\hrprinter
\\printserver1\copier1

If the name of the server matches the variable “from_sv” (line 14) in the script, the printer is removed from the user and a new printer of the same name is added using the “to_sv” variable (line 15) for example if the “to_sv” variable was set to “\\newprintserver;

\\newprintserver\hrprinter
\\newprintserver\copier1

The script does not create a queue on the new print server it just removes any reference to the old print server and as long as a queue exists on the new server it adds the printer back on for the user pointing at the new server.

To move all your printer queues, drivers and ports from one server to another take a look at Microsoft’s free tool Print Migrate

This script works great if called from a login script! And works fine on Citrix

Sometimes doing a big bang migration from an old print server to a new one is not the best idea, if you would prefer a more granular approach based on the printer model or queue name look at this script of mine. Or I have another script that could be used to just audit what printers users have as part of a planning process.

Dim from_sv, to_sv, PrinterPath, PrinterName, DefaultPrinterName, DefaultPrinter
Dim DefaultPrinterServer, SetDefault, key
Dim spoint, Loop_Counter
Dim WshNet, WshShell
Dim WS_Printers

DefaultPrinterName = ""

spoint = 0
SetDefault = 0

set WshShell = CreateObject("WScript.shell")

from_sv = "\\printserver1" 'This should be the name of the old server.
to_sv = "\\newprintserver" 'This should be the name of your new server.

' Make sure there are printers installed
On Error Resume Next
key = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
DefaultPrinter = LCase(WshShell.RegRead (key))

If Err.Number <> 0 Then
    DefaultPrinterName = ""
else
    'If the registry read was successful then parse out the printer name so we can
    ' compare it with each printer later and reset the correct default printer
    ' if one of them matches this one read from the registry.

    spoint = instr(3,DefaultPrinter,"\")+1
    DefaultPrinterServer = left(DefaultPrinter,spoint-2)

    if DefaultPrinterServer = from_sv then
        DefaultPrinterName = mid(DefaultPrinter,spoint,len(DefaultPrinter)-spoint+1)
    end if 
end if

Set WshNet = CreateObject("WScript.Network")
Set WS_Printers = WshNet.EnumPrinterConnections

For Loop_Counter = 0 To WS_Printers.Count - 1 Step 2

    PrinterPath = lcase(WS_Printers(Loop_Counter + 1))

    if LEFT(PrinterPath,len(from_sv)) = from_sv then
        spoint = instr(3,PrinterPath,"\")+1
        PrinterName = mid(PrinterPath,spoint,len(PrinterPath)-spoint+1)

        WshNet.RemovePrinterConnection from_sv+"\"+PrinterName
        WshNet.AddWindowsPrinterConnection to_sv+"\"+PrinterName

        if DefaultPrinterName = PrinterName then
            WshNet.SetDefaultPrinter to_sv+"\"+PrinterName
        end if
    end if
Next

Set WS_Printers = Nothing
Set WshNet = Nothing
Set WshShell = Nothing

16 thoughts on “Move Users to a New Print Server [Script]”

  1. This works great. Only thing I need to figure out is how to inject an option to move users to a printer that did not exist before if the were mapped to a now obsolete printer.

    example
    Users were mapped to \\oldprintserver\hp23bw
    need these users to now mappted to \\newprintserver\hp23color

    Reply
  2. Hello

    I want use this script, but is it possible that when the script is finished with runnig to send a msgbox ” Migration done”. And the 2nd request, to have a log file with the name of hostname and saved on a shardrive.

    Reply
  3. Hi

    I want use this script but i need a msg box at the end of the script the migration is done. Is it possible to create a log file with the hostname as file name and save it on a sharedrive with content removed printer and installed printer.

    Reply
  4. I was looking for a simple script to update Win7 clients with a new network printer server name.
    Your script works with Win7 and does the trick, very well done and thanks for sharing!

    Reply
  5. I ran this script on the client computer and it worked but it created a redirect on both servers of the printers that were reinstalled. When I remove a printer from the client the redirect goes away on the server but if I install a printer on the client, the redirect comes back. Any ideas? Thanks

    Reply
  6. Is there any way we can get a verbose mode to see what’s doing? or logs so we can track what the script has done or any errors?

    Thank you!

    Reply
    • Hi Ricardo,

      The easiest method would be to write to a text file, either on a network share or on the local drive.

      Here is an example script to write to a log text file.

      ‘ Top of the script
      Dim objFSO, logFile

      ‘Flags for files
      Const ForReading = 1
      Const ForWriting = 2
      Const ForAppending = 8

      logFile = “\\printerserver\share\logfiles.txt”
      Set objFSO = CreateObject(“Scripting.FileSystemObject”)
      Set objLogFile = objFSO.CreateTextFile(logFile, ForAppending)

      ‘ Add lines to the log file
      objLogFile(“Log Line 1” & chr(13) & chr(10))
      objLogFile(“Log Line 2” & chr(13) & chr(10))

      Reply

Leave a Reply to Karuna Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.