January 9th, 2006

How Can I Remove All the Black-and-White Printers on a Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! On a Windows XP computer, how can I remove all the black-and-white printers, leaving only color printers?

— JL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JL. You know, in one classic episode of the Simpsons, Bart, in the throes of a sugar high, joins the Junior Campers. The next morning, realizing what he has done, he vows to weasel out of his commitment. His mother tries to stop him, but Homer interrupts: “Learning how to weasel out of things is important,” he says. “It’s what separates us from the animals … except the weasel, of course.”

As you might have guessed, the Scripting Guys are about to try and separate ourselves from the animals. Not that we’re going to totally weasel out of answering the question; heaven forbid! It’s just that we’re going to stick very close to the actual wording of the question, and only explain how to do this on Windows XP and Windows Server 2003.

Why are the Scripting Guys – of all people – suddenly sticklers for protocol? Well, as it turns out, removing just the black-and-white printers on a Windows XP or Windows 2003 computer is pretty easy. That’s not the case on, say, Windows 2000. On that version of Windows you can delete printers on the local computer, but you can’t (at least not easily) delete printers on remote computers. Trying to do things remotely that you can’t really do remotely always leads to far too many problems for us to address in this column. Therefore, we’ll just answer the question at hand – how do I do this on Windows XP? – and pretend we never even thought about how (or if) this might work on Windows 2000.

Note. OK, OK. Let us know if you need to perform this task on Windows 2000; if there’s enough interest we’ll reconsider our weasel-like behavior.

With that in mind, here’s the answer to your question, JL:

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

Set colPrinters = objWMIService.ExecQuery _ (“Select * from Win32_PrinterConfiguration Where Color = 1”)

For Each objPrinter in colPrinters strName = objPrinter.Name strName = Replace(strName, “\”, “\\”)

Set colBWPrinters = objWMIService.ExecQuery _ (“Select * from Win32_Printer Where Name = ‘” & strName & “‘”) For Each objBWPrinter in colBWPrinters objBWSPrinter.Delete_ Next Next

As you can see, after all that buildup there really isn’t all that much to the script. We start off by binding to the WMI service on the local computer, although – as hinted at above – we could just as easily perform this task on a remote computer. We then use this line of code to connect to the Win32_PrinterConfiguration class and retrieve a collection of all the printers on the computer that do not print color:

Set colPrinters = objWMIService.ExecQuery _
    (“Select * from Win32_PrinterConfiguration Where Color = 1”)

That might sound a bit backwards, but, remember, we want to keep all the color printers. The easiest way to do that is to grab a collection of all the black-and-white printers and then delete each printer in the collection. Do that, and, when you’re done, all you’ll have left are color printers.

We’re willing to bet your average weasel would never have thought of a solution like that.

Hey, we said your average weasel.

As you probably already figured out, if the value of the Color property is 1 then the printer is a black-and-white printer. If the value is 2, then you have a color printer. Those, by the way, are the only possible values for the Color property

After we get back our collection we then set up a For Each loop to walk through all the items in the collection. For the first item (that is, our first black-and-white printer) we use this line of code to retrieve the printer name and store it in a variable named strName:

strName = objPrinter.Name

That brings us to this line of code:

strName = Replace(strName, “\”, “\\”)

What we’re doing here is replacing any \ characters in the printer name with \\. Why the heck do we do that? Well, if we have a network printer it’s going to have a name like this: \\atl-ps-01\finance_printer. That’s fine, but we need to use the printer name in a WQL query. Because the \ is a reserved character in WQL we need to “escape it” (double it up). In other words, in our query the printer name needs to look like this: \\\\atl-ps-01\\finance_printer. That’s what we use the Replace function for: it replaces (escapes) each instance of the \ with \\.

Got that? Good. Now, as it turns out, the Win32_PrinterConfiguration class can tell us whether or not a printer can print in color. What it can’t do, however, is delete a printer. To actually get rid of the black-and-white printers we need to connect to the Win32_Printer class, which is what we do here:

Set colBWPrinters = objWMIService.ExecQuery _
        (“Select * from Win32_Printer Where Name = ‘” & strName & “‘”)

Notice that we use a Where clause here, indicating that we only want to retrieve those printers where the Name is equal to the variable strName. (And, remember, we just doubled up all the \ characters in strName.) Because printer names must be unique on a computer this ensures that we will get back a collection consisting of just one printer.

And what do we do with that collection? That’s easy. We set up a For Each loop to walk through it, something we have to do even though there’s only one item in the collection. Inside that For Each loop we call the Delete_ method to delete the printer:

objBWSPrinter.Delete_

Note. Incidentally, this is why our script won’t work on Windows 2000: on versions of Windows prior to Windows XP, the Win32_Printer class does not support the Delete_ method.

That single line of code deletes our first black-and-white printer. We then loop around to our original For Each loop and repeat the process with the second printer in the collection. This continues until all the black-and-white printers have been deleted.

And that’s the kind of thing that separates the Scripting Guys from the animals … except the Australian scripting badger, of course. But other than that ….

Author

0 comments

Discussion are closed.