{"id":70983,"date":"2004-11-17T04:21:00","date_gmt":"2004-11-17T04:21:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/17\/hey-scripting-guy-is-there-any-way-to-determine-the-default-printer-on-a-computer\/"},"modified":"2004-11-17T04:21:00","modified_gmt":"2004-11-17T04:21:00","slug":"hey-scripting-guy-is-there-any-way-to-determine-the-default-printer-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-is-there-any-way-to-determine-the-default-printer-on-a-computer\/","title":{"rendered":"Hey, Scripting Guy! Is There Any Way to Determine the Default Printer On a Computer?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! Is there any way to determine the default printer on a computer?<BR><BR>&#8212; JW<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JW. If you\u2019re running Windows XP or Windows Server 2003, determining the default printer is easy. That\u2019s because the WMI class Win32_Printer found on these versions of Windows includes a property named <B>Default<\/B>. 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\u2019s FALSE, well, then it\u2019s 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):<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where Default = TRUE&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    Wscript.Echo objPrinter.ShareName\nNext\n<\/PRE>\n<P>And to think some people <I>still<\/I> say there\u2019s no reason to upgrade to Windows XP or Windows 2003!<\/P>\n<P>Of course, some of you are a bit worried right now. \u201cThey said this was easy <I>if<\/I> you\u2019re running Windows XP or Windows 2003. They didn\u2019t say anything about Windows 2000. I\u2019m running Windows 2000; does this mean that either I can\u2019t do this at all, or that it\u2019s going to be really hard?\u201d<\/P>\n<P>Hey, relax: you can use WMI to determine the default printer on Windows 2000. And it\u2019s not really hard, it\u2019s just a little tricky. That\u2019s because on Windows 2000 (and Windows NT 4.0 and Windows 98) the Win32_Printer class doesn\u2019t have the Default property. Thus you can\u2019t simply query for printers with a Default value of TRUE.<\/P>\n<P>But that\u2019s OK. The Win32_Printer class found on these older versions of Windows <I>does<\/I> include a bitmask property named <B>Attributes<\/B>. We can\u2019t even begin to explain bitmasks today (for more information, see this section of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_scr_tspz.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>), 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\u2019s off, it\u2019s not.<\/P>\n<P>That\u2019s a long way of saying this: to determine the default printer on a Windows 2000 computer, we simply need to grab the collection of <I>all<\/I> 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\u2019s our default printer.<\/P>\n<P>Here\u2019s the script that does just that:<\/P><PRE class=\"codeSample\">Const DEFAULT = 4<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    If objPrinter.Attributes And 4 Then \n        Wscript.Echo objPrinter.ShareName\n    End If\nNext\n<\/PRE>\n<P>If you aren\u2019t familiar with bitmasks, then this script might not make too much sense to you. But that\u2019s OK; go ahead and use it now, and then read up on bitmasks when you get the chance.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way to determine the default printer on a computer?&#8212; JW Hey, JW. If you\u2019re running Windows XP or Windows Server 2003, determining the default printer is easy. That\u2019s because the WMI class Win32_Printer found on these versions of Windows includes a property named Default. As the name implies, this [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[445,404,3,5],"class_list":["post-70983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there any way to determine the default printer on a computer?&#8212; JW Hey, JW. If you\u2019re running Windows XP or Windows Server 2003, determining the default printer is easy. That\u2019s because the WMI class Win32_Printer found on these versions of Windows includes a property named Default. As the name implies, this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70983\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}