Skip to content

Instantly share code, notes, and snippets.

@danielwestendorf
Created September 15, 2011 17:59
Show Gist options
  • Save danielwestendorf/1219973 to your computer and use it in GitHub Desktop.
Save danielwestendorf/1219973 to your computer and use it in GitHub Desktop.
Printer Magic VBScript
On Error Resume Next
'Save as a file with the .vbs extension such as printerConversion.vbs
'Script written by Daniel Westendorf - daniel at prowestech dot com. It is provided to be used and minipulated at
'will, but with any expressed warranty as to what it will or will not do. No support is provided with this script.
'Usage:
'Save as a file with the .vbs extension such as printerConversion.vbs
'Replace PATHTTOPRINTERMAPPINGCSVFILE with the real path to your mapping file. It should be in a location accessible
'by any machine such as \\domanNAME\NETLOGON
Dim objNetwork
Dim strComputer
Dim objWMIService
Dim objPrinter
Dim colInstalledPrinters
Dim defaultPrinter
Dim fs
Dim csvFile
Dim logFile
Dim printerArray
Dim printerChange
Dim i
printerArray = Array()
set fs = CreateObject("Scripting.FileSystemObject")
set csvFile = fs.OpenTextFile("PATHTTOPRINTERMAPPINGCSVFILE") 'Path to CSV file with format of \\OLDPRINTSERVER\PRINTER, \\NEWPRINTSERVER\REPLACEMENTPRINTQUEUE
i = 0
'Read CSV file and create MultiDimensional Array
Do Until csvFile.AtEndOfStream
ReDim Preserve printerArray(i)
printerChange = split(csvFile.Readline, ",") 'array of old, new printers, split by the comma
printerArray(i) = Array(printerChange(0), printerChange(1)) 'add printerChange to printerArray array at the next open slot
i = i + 1
Loop
csvFile.Close
'Create or open logfile, write date
If fs.FileExists("C:\PrinterSwap.log") Then
Set logFile = fs.OpenTextFile("C:\PrinterSwap.log", 8)
Else
Set logFile = fs.CreateTextFile("C:\PrinterSwap.log", 2)
End If
LogFile.WriteLine ("************************")
logFile.WriteLine ("------------------------")
logFile.WriteLine (Now)
logFile.WriteLine ("------------------------")
strComputer = "."
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 'loop through printers installed on system
For Each printerChange in printerArray 'for each printer, loop through the printerArray searching for a match
if LCase(objPrinter.Name) = LCase(printerChange(0)) Then 'check for match
objPrinter.Delete_
If Err.Number <> 0 Then
logFile.WriteLine ("Removed: " & objPrinter.Name)
Else
logFile.WriteLine ("Error removing printer: " & objPrinter.Name)
End If
Err.Clear
objNetwork.AddWindowsPrinterConnection(printerChange(1)) 'create new printer connection
If Err.Number <> 0 Then
logFile.WriteLine ("Added: " & printerChange(1))
Else
logFIle.WriteLine ("Error adding printer: " & printerChange(1))
End If
Err.Clear
if objPrinter.Default = "True" Then 'was that printer the default printer? Will only return true once.
defaultPrinter = printerChange(1)
End If
Err.Clear
End If
Next
Next
If defaultPrinter <> Empty Then
objNetwork.SetDefaultPrinter(defaultPrinter) 'set the default printer
If Err.Number <> 0 Then
logFile.WriteLine ("Set Default printer: " & defaultPrinter)
Else
logFile.WriteLine ("!!!Couldn't set default printer!!!")
End If
End If
LogFile.WriteLine ("************************")
logFile.Close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment