We have a fantastic VB script that adds multiple network printers, then goes on to select one of them to be the default.

Dim multiPrinter, UNCpath1, UNCpath2, UNCpath3
UNCpath1 = "\\server\printer1"
UNCpath2 = "\\server\printer2"
UNCpath3 = "\\server\printer3"
Set multiPrinter = CreateObject("WScript.Network")
multiPrinter.AddWindowsPrinterConnection UNCpath1
multiPrinter.AddWindowsPrinterConnection UNCpath2
multiPrinter.AddWindowsPrinterConnection UNCpath3

Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\server\printer1"
PrinterDriver = "PrinterDriver"
WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
WshNetwork.SetDefaultPrinter "\\server\printer1"
WScript.Quit

This works perfectly- the only problem is that it does not delete the existing network printers first.

That is done separately by a batch file (causing a Command Prompt window to flash on the screen at logon).

reg delete "hkcu\printers\connections" /f

Please could someone advise me as to how the functionality of the batch file can be included in the VB script.

It's quite a basic question- but I am new to VB scripting. Thanks.

  • Have you considered moving to Group Policy Preferences for Printer Mappings? It's generally far superior and much easier to work with. – Dan Jul 17 '13 at 11:44
  • That was my first choice. Unfortunately I don't believe this is possible with SBS 2003 R1. It looks like we need to be running at least SBS 2003 R2 in order to see the "Printer Management" console: social.technet.microsoft.com/Forums/windowsserver/en-US/…. (I am assuming you need that console to deploy printers directly via Group Policy- correct me if I'm wrong.) – Austin ''Danger'' Powers Jul 17 '13 at 11:59
up vote 1 down vote accepted

I eventually managed to find what I was looking for with a Google search:

http://community.spiceworks.com/topic/128389-is-it-possible-to-delete-all-offline-network-printers-with-vbscript-on-logon

The VB script I needed was this:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = True")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next

I have tested it thoroughly and it leaves all PDF printers and locally-installed printers unchanged- just targeting network printers as I wanted.

Hopefully this helps other people who are trying to achieve a really slick, Group Policy-deployed printer setup with no Command Prompt windows flashing on the screen at logon.

Note: due to the mixed nature of our environment (XP, Win7 x86 and Win7 x64) I logged on as admin myself on each PC once to make sure the appropriate drivers for all 3 printers were installed successfully. So I am not relying on this script to install the drivers (although it can easily be tailored to do that). Our 2 Win7 x64 machines could not pull the right drivers from the network for some reason (perhaps because printers shared on an XP x86 machine?) By logging in as admin (and manually pointing to the correct .ini file where needed) I was able to ensure users had no unexpected driver issues. The script just ensures the correct print queues are shown for each user on each PC. I was not sure how to tailor the script to pick the right driver version for multiple OS types, and did not want to leave anything to chance.

Anyway, all combined, the entire VB script does the following 3 things at logon:

1) Deletes all existing print queues in that user profile:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = True")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next

2) Adds multiple print queues as required:

Dim multiPrinter, UNCpath1, UNCpath2, UNCpath3
UNCpath1 = "\\server\printer1"
UNCpath2 = "\\server\printer2"
UNCpath3 = "\\server\printer3"
Set multiPrinter = CreateObject("WScript.Network")
multiPrinter.AddWindowsPrinterConnection UNCpath1
multiPrinter.AddWindowsPrinterConnection UNCpath2
multiPrinter.AddWindowsPrinterConnection UNCpath3

3) Sets the correct default printer:

Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\server\printer1"
PrinterDriver = "PrinterDriver"
WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
WshNetwork.SetDefaultPrinter "\\server\printer1"
WScript.Quit

Enjoy!

You can give this a try:

Set wshPrn = multiPrinter.EnumPrinterConnections
For x = 0 To wshPrn.Count - 1 Step 2
  If Left(wshPrn.Item(x + 1), 2) = "\\" Then multiPrinter.RemovePrinterConnection wshPrn.Item(x + 1), True, True
Next

or At the top of your batch file make sure you have and go to the link to run the bat file minimized

@echo off

http://www.computerhope.com/issues/ch000932.htm

Good Luck!

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.