Need to modify VB script so it deletes all existing network printers first

11,680

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!

Share:
11,680

Related videos on Youtube

Austin ''Danger'' Powers
Author by

Austin ''Danger'' Powers

Scott Evil: I hate you! I hate you! I wish I was never artificially created in a lab! Dr. Evil: Scott, that hurts daddy when you say that. Honestly.

Updated on September 18, 2022

Comments

  • Austin ''Danger'' Powers
    Austin ''Danger'' Powers over 1 year

    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.

    • Dan
      Dan almost 11 years
      Have you considered moving to Group Policy Preferences for Printer Mappings? It's generally far superior and much easier to work with.
    • Austin ''Danger'' Powers
      Austin ''Danger'' Powers almost 11 years
      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
    Austin ''Danger'' Powers almost 11 years
    I tried adding that to the top of my VB script but got an error when logging on with a test account. "Error: Object required. Code: 800A01A8. Source: Microsoft VBScript runtime error". Any ideas?
  • Zoredache
    Zoredache almost 11 years
    @msindle, Your code fragement eould have been better with more context. It isn't obvious that you are using a WshNetwork object. For others, see devguru.com/technologies/wsh/17396 devguru.com/technologies/wsh/17406