shared printer change script

Every network admin has had to remove a print server from their environment and set up a new one. While installing a print server can be tedious job. the worst part for me has always been updating the installed printers on all of my clients.  well with this little vbscript it is no problem at all!

On Error Resume Next
Function GetDefaultPrinter()
sRegVal = “HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device”
sDefault = “”
On Error Resume Next
sDefault = objShell.RegRead(sRegVal)
sDefault = Left(sDefault ,InStr(sDefault, “,”) – 1)
On Error Goto 0
GetDefaultPrinter = sDefault
End Function

Set objNetwork = CreateObject (“Wscript.Network”)
Set objShell = CreateObject (“WScript.Shell”)
Set objFSO = CreateObject (“Scripting.FileSystemObject”)

LogonServer = objShell.ExpandEnvironmentStrings(“%logonserver%”)
UserName = objShell.ExpandEnvironmentStrings(“%username%”)

strComputer = “.”
PrintServer = “NEW PRINT SERVER NAME HERE”
PrintServer = LCase (PrintServer)

Err.Clear
Set objWMIService = GetObject(“winmgmts:” & _
“{impersonationLevel=Impersonate}!\\” & strComputer & “\root\cimv2”)

If Err.Number Then
wscript.echo (“Error : ” & Err.Number & “: ” & Err.Description & VbCrLf)
Err.Clear

Else
ImpDefault = GetDefaultPrinter
Set colInstalledPrinters = objWMIService.ExecQuery _
(“SELECT * FROM Win32_Printer”)
For Each objPrinter in colInstalledPrinters
PrinterArray = Split (objPrinter.Name , “\”)

If (LCase(objPrinter.ServerName) <> “”) and (LCase(objPrinter.ServerName) <> “\\” & PrintServer) then

objNetwork.AddWindowsPrinterConnection “\\” & PrintServer & “\” & PrinterArray(3)
If Err.Number Then
wscript.echo (“Error : ” & Err.Number & “: ” & Err.Description & VbCrLf)
Err.Clear
End If

If ImpDefault = objPrinter.Name then
objNetwork.SetDefaultPrinter (“\\” & PrintServer & “\” & PrinterArray(3))
End If

objNetwork.RemovePrinterConnection objPrinter.Name
End If
Next
End If