Need something adding to VBScript for Adding and Deleting Printers

Posted on 2006-04-11
Last Modified: 2010-04-07
Basically i have some code for a VBScript which adds printers to a clients PC that refers to the print server at the location.

We have an old print server (ICHOLS22) which is being replaced by the new one (GBHOLFP01)

Here is the script i have so far:

On Error Resume Next

Dim net
Dim NetPrinter
Dim Printer


Set net = 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
        Printer = UCase(
        If Printer <> "\\GBHOLFP01\PRN1918036" then
            net.RemovePrinterConnection printer
        end If

net.AddWindowsPrinterConnection "\\GBHOLFP01\PRN1918036"
net.AddWindowsPrinterConnection "\\GBHOLFP01\PRN5500380"
net.SetDefaultPrinter "\\GBHOLFP01\PRN5500380"

What i need to add to this is a removal of any printer from the clients PC that refers to the old ICHOLS22 printer.

Can anyone help?

Question by:PhilJMcDougall
    LVL 70

    Expert Comment

    by:Chris Dent

    You already have it set to Remove any Printer Connection not called "\\GBHOLFP01\PRN1918036". If that's not working then remove the line that says "On Error Resume Next" and re-run the script.


    LVL 1

    Author Comment

    Yes that works already the bit where i have said don't remove the selected printer, problem is they have other printers on there (random ones that i can't add into the script) from other print servers so i just want to remove printers that recide on ICHOLS22.

    net.RemovePrinterConnection "\\ICHOLS22\PRN00500000"
                                               ^ this part to remove
    LVL 6

    Accepted Solution

    add this for the printers you wish to remove.

    If Printer <> "\\GBHOLFP01\PRN1918036" then
                net.RemovePrinterConnection "\\ICHOLS22\PRN00000001"
                net.RemovePrinterConnection "\\ICHOLS22\PRN00000002"
                net.RemovePrinterConnection "\\ICHOLS22\PRN00000003"
                net.RemovePrinterConnection "\\ICHOLS22\PRN00000004"
    end If

    quick fix
    LVL 1

    Author Comment

    Worked a treat. cheers Zell71!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now