Hey, Scripting Guy! Is There Any Way to Determine the Default Printer On a Computer?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way to determine the default printer on a computer?

— JW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JW. If you’re running Windows XP or Windows Server 2003, determining the default printer is easy. That’s because the WMI class Win32_Printer found on these versions of Windows includes a property named Default. As the name implies, this property tells you which printer is the default printer: if the value is TRUE the device is the default printer, if it’s FALSE, well, then it’s not. To find out the default printer on a computer all you have to do is query for the collection of all printers where the Default property is TRUE (because you can have only one default printer, the collection will have only one item):

strComputer = “.”

Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”) Set colPrinters = objWMIService.ExecQuery _ (“Select * From Win32_Printer Where Default = TRUE”)

For Each objPrinter in colPrinters Wscript.Echo objPrinter.ShareName Next

And to think some people still say there’s no reason to upgrade to Windows XP or Windows 2003!

Of course, some of you are a bit worried right now. “They said this was easy if you’re running Windows XP or Windows 2003. They didn’t say anything about Windows 2000. I’m running Windows 2000; does this mean that either I can’t do this at all, or that it’s going to be really hard?”

Hey, relax: you can use WMI to determine the default printer on Windows 2000. And it’s not really hard, it’s just a little tricky. That’s because on Windows 2000 (and Windows NT 4.0 and Windows 98) the Win32_Printer class doesn’t have the Default property. Thus you can’t simply query for printers with a Default value of TRUE.

But that’s OK. The Win32_Printer class found on these older versions of Windows does include a bitmask property named Attributes. We can’t even begin to explain bitmasks today (for more information, see this section of the Microsoft Windows 2000 Scripting Guide), but suffice to say that a single bitmask is similar to a control panel with a bank of switches. Each switch represents, in this case, the property of a printer. If the switch is on, then that property is TRUE. If the switch is off, that property is FALSE. In the case of Win32_Printer, the bit (switch) equal to 4 represents the Default property. If this bit is on, the printer is the default printer; if it’s off, it’s not.

That’s a long way of saying this: to determine the default printer on a Windows 2000 computer, we simply need to grab the collection of all the computers, and then inspect the Attributes property on each one. When we find a printer where the bit equal to 4 is on, then that’s our default printer.

Here’s the script that does just that:

Const DEFAULT = 4

strComputer = “.”

Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”) Set colPrinters = objWMIService.ExecQuery _ (“Select * From Win32_Printer”)

For Each objPrinter in colPrinters If objPrinter.Attributes And 4 Then Wscript.Echo objPrinter.ShareName End If Next

If you aren’t familiar with bitmasks, then this script might not make too much sense to you. But that’s OK; go ahead and use it now, and then read up on bitmasks when you get the chance.

0 comments

Discussion is closed.

Feedback usabilla icon