Delete Particular Mapped Network Printers with Scripts.

Posted on 2007-10-02
Last Modified: 2011-08-18

May be this would be sort of repeated question. But i cannot find the exact solution for my problem.

To say, we are migrating from an Old Print Server (Win2K) to a New Print Server (Win2K3 R2). All clients are Win2K. Clients have different printers mapped as per their location in different buildings. I need to create a script that would allow me to delete only those printers which i mention, NOT ALL. I have script to map the network printers i want and works fine. I also got one Script as below that helps me to delete only one network printer.
Option Explicit
Dim DelPrint, UNCpath
UNCpath = "\\Server1\Printer"
Set DelPrint = WScript.CreateObject("WScript.Network")
DelPrint.RemovePrinterConnection UNCpath, true, true
Wscript.Echo "This printer was removed : " & UNCpath

' End of Example Script to remove printer

This only allows me to enter ONE uncpath.
- I want a script that will allow me to mention all the network printers to be deleted in a single script.
- If the printers mentioned in the script are not present, it will ignore, else shall delete that printer. No error messages if any printer mentioned in the script is not found on the Client PC.

I hope i am clear and request to please guide me.


Question by:suhailkardekar
    LVL 38

    Accepted Solution

    Option Explicit
    Dim arrDelPrint, objNetwork, objWMIService, colPrinters, objPrinter, strPrinter

    'Add the UNC paths of all printers to remove here
    arrDelPrint = Array("\\server1\printer1","\\server1\printer2","\\server2\printer1")

    Set objNetwork = CreateObject("Wscript.Network")

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
    For Each objPrinter in colPrinters
       'Checks that the network printer exists before unmapping it.
       If Not objPrinter.Attributes And 64 Then
          For Each strPrinter in arrDelPrint
             If LCase(strPrinter) = LCase(objPrinter.ServerName & "\" & objPrinter.ShareName) Then
                objNetwork.RemovePrinterConnection strPrinter
             End If
       End If

    Author Comment


    Thats exactly what i needed.

    Thanks a thousandfold.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now