How-to: Re-Install Existing Printers on Windows using VBScript
Preferred Editor: Notepad++
Microsoft VBScript: Language Reference
Sometimes Windows, for reasons unknown, gets confused about printers. This is a problem in an office environment where people are printing almost non-stop. At my work we have about thirty employees, and in approximately six months, they’ve printed over 100 000 pages. I know this because one of the ink cartridges in the printer was recently replaced, and the printer keeps tabs on these kind of stats.
Anyway, an issue we sometimes encounter is that peoples printers simply don’t work. When they’re selected in the print dialogue of whatever application, the application locks up while the operating system tries to remember where the printer really is. I have no idea what causes this; if you do, please drop me a comment. In an effort to avoid this kind of behaviour, I’ve made an addition to our login script that removes and re-installs all the printers on the system. Hopefully by keeping the printers “fresh” in this way, I can avoid having peoples’ time wasted by silly, preventable printer problems.
The Script
-
-
‘setup-printers.vbs
-
‘deletes and re-adds printers that already exist on the client system
-
Set objNetwork = CreateObject("WScript.Network")
-
‘the EnumPrinterConnections method returns an array containing the printer name and its UNC connection string
-
‘ood array indices are the names, even array indices are the connection strings
-
Set printers = objNetwork.EnumPrinterConnections
-
‘For all printers
-
For i = 0 to printers.Count-1 Step 2
-
Dim currentPrinter
-
‘Remove
-
currentPrinter = printers.Item(i+1)
-
If Left(currentPrinter, 2) = "\\" Then
-
‘ we have a network printer
-
On Error Resume Next
-
objNetwork.RemovePrinterConnection printers.Item(i+1)
-
Else
-
‘do nothing
-
End If
-
‘Re-add
-
currentPrinter = printers.Item(i+1)
-
If Left(currentPrinter, 2) = "\\" Then
-
‘ we have a network printer
-
On Error Resume Next
-
objNetwork.AddWindowsPrinterConnection printers.Item(i+1)
-
Else
-
‘do nothing
-
End If
-
Next
-
‘ setup the Edgeline as default printer
-
objNetwork.SetDefaultPrinter "\\springwood\HP CM8060 MFP with Edgeline PS"
-
Essentially, what the code does is grab the existing printer connections, delete them, and then re-add them. The in-code comments are enough to satisfy anyone with a web browser capable of looking up Microsoft’s VBScript references. What may not be immediately obvious is the last part — the script will re-add them in the order they existed, but without further input, will make the last added printer the default printer. This isn’t what we’re after at my workplace, therefore we need to fix that. However, I’ve just figured that in my particular instance I could re-add the printers in reverse — problem solved!












2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]