How can I use a script to automatically replace a printer connection with another?

A. The following code, which you can download here , searches for a connected network printer and replaces it with an alternate:

Option Explicit
Dim objWMIService, objPrinter, colItems, strComputer, objWshShell, 

strComputer ="."

Set objWshShell = WScript.CreateObject("WScript.Shell")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
' For Remote:
' Set Locator = CreateObject("WbemScripting.SWbemLocator") 
' Set objWMIService = Locator.ConnectServer(strComputer, "root\cimv2", 
  strUserName, strPassword) 

Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")

' On Error Resume Next
For Each objPrinter In colItems
If UCase(objPrinter.DeviceID) = UCase("\\savdalprinterOld\printer1") Then
   Wscript.Echo "Found match " & objPrinter.DeviceID & ", replacing"
   ' Add new printer. Need the ,1,true to wait for shell to complete before continue
   objWshShell.Run "rundll32 printui.dll,PrintUIEntry /in /Gw /q /n \\savdaldc01\printerNew",1, true

   ' Remove the old
   objWshShell.Run "rundll32 printui.dll,PrintUIEntry /dn /q /n \\savdalprinterOld\printer1",1, true

   If objPrinter.Default Then ' if it's the default set as default
     objWshShell.Run "rundll32 printui.dll,PrintUIEntry /y /n \\savdaldc01\printerNew",1, true
   End If

End If 


You can change the printer the code searches for and what it maps to when it finds a match.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.