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

  1.  
  2. ‘setup-printers.vbs
  3. ‘deletes and re-adds printers that already exist on the client system
  4. Set objNetwork = CreateObject("WScript.Network")
  5. ‘the EnumPrinterConnections method returns an array containing the printer name and its UNC connection string
  6. ‘ood array indices are the names, even array indices are the connection strings
  7. Set printers = objNetwork.EnumPrinterConnections
  8. ‘For all printers
  9. For i = 0 to printers.Count-1 Step 2
  10.     Dim currentPrinter
  11.     ‘Remove
  12.     currentPrinter = printers.Item(i+1)
  13.         If Left(currentPrinter, 2) = "\\" Then
  14.         ‘ we have a network printer
  15.         On Error Resume Next
  16.         objNetwork.RemovePrinterConnection printers.Item(i+1)
  17.         Else
  18.             ‘do nothing
  19.         End If
  20.     ‘Re-add
  21.     currentPrinter = printers.Item(i+1)
  22.         If Left(currentPrinter, 2) = "\\" Then
  23.         ‘ we have a network printer
  24.         On Error Resume Next
  25.         objNetwork.AddWindowsPrinterConnection printers.Item(i+1)
  26.         Else
  27.         ‘do nothing
  28.         End If
  29. Next
  30. ‘ setup the Edgeline as default printer
  31. objNetwork.SetDefaultPrinter "\\springwood\HP CM8060 MFP with Edgeline PS"
  32.  

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!


About this entry