How Can I Remove All the Local Printers from a Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I remove all the local printers from a computer?

-- JW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JW. You did say that you wanted to do this only on computers running Windows XP or Windows Server 2003, didn’t you? You didn’t? Oh. Well, we’re going to pretend that you did, because this is a very easy task to carry out on Windows XP and Windows Server 2003. That’s because on Windows XP and Windows Server 2003 the WMI class Win32_Printer not only can easily distinguish between local printers and network printers, but it also supports the Delete_ method. As you can probably guess, that method enables you to delete printer connections. In other words, WMI lets you pick out and remove local printers. Which, all in all, sounds exactly like the answer to your question.

That’s the good news. The bad news is that, on earlier versions of Windows, it’s more complicated to pick out local printers; even worse, the Win32_Printer class doesn’t support the Delete_ method. That means it’s far more challenging to identify and remove local printers. Because of that we’re going to focus on removing local printers from Windows XP and Windows Server 2003 computers and hope that takes care of your problem. If not, we’ll have to tackle printer removal on Windows 2000 computers at a later date.

For Windows XP and Windows Server 2003 users you can delete all the local printers using a script as simple as this:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = FALSE")

For Each objPrinter in colInstalledPrinters

This script begins by connecting to the WMI service on the local computer (although it can be easily modified to remove printers from a remote computer instead). We then use this line of code to return a collection of all the local printers; as far as WMI is concerned, a printer is a local printer if the Network property is set to False:

Set colInstalledPrinters =  objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = FALSE")

Once we have a collection of local printers we simply set up a For Each loop to walk through that collection, calling the Delete_ method (note the underscore on the end) for each printer in the collection:

For Each objPrinter in colInstalledPrinters

There you have it: that will delete all the local printers, while leaving the network printer connections alone. Now aren’t you glad that you asked about removing local printers specifically from Windows XP and Windows Server 2003 computers without giving any thought whatsoever to earlier versions of the operating system?

OK, fine: we’ll see what we can do about addressing this issue on Windows 2000 computers. We won’t make any promises, but we’ll look into it.

Comments (9)

  1. Anonymous says:

    When i try and run the script using window 7 with PowerShell I get the following error message:

    Unexpected token '&' in expression or statement.

    At H:MyScriptsprintertest1.ps1:2 char:46

    + Set objWMIService = GetObject("winmgmts:\" & <<<<  strComputer & "rootcimv2")

       + CategoryInfo          : ParserError: (&:String) [], ParseException

       + FullyQualifiedErrorId : UnexpectedToken

  2. kev says:

    How do you do it on windows 7

  3. Bigbash says:

    Kev it works, just tested it!!

  4. grim says:

    How would I put in an exception? Say I don't want to delete the "AdobePDF" printer but I do want to delete all others.

  5. Jareem Gamble says:

    Thanks man, I used this script with altiris. Works Great!

  6. ImmI says:

    Hi its a great script, i have one puzzle

    I dont wants to delete the printer where port is like PDF%, how can i can incorporate this in the script so that it do not delete any printer whose port name contains PDF in it. because i wants to clean up all exept PDF printer.

  7. VulcanJedi says:

    For Each objPrinter in colInstalledPrinters

       if  not instr(1,,"pdf",vbtextcompare)  then objPrinter.Delete_


  8. Ron says:

    Does anyone know why this script would work fine in windows 7, but give an error on the odjPrinter.Delete_ line when run under XP?

    The error says not found, the code is 80041002, and the source is SWbemObjectEx

  9. Thiago Di Giorgio says:

    Hey all, I fixed the script to keep the PDF printers, follow the loop.

    For Each objPrinter in colInstalledPrinters

    if (instr(1,,"PDF",1) = 0) then objPrinter.Delete_


    This will keep the pdf printer from into the collection you are using.

Skip to main content