{"id":69363,"date":"2005-07-19T17:05:00","date_gmt":"2005-07-19T17:05:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/19\/how-can-i-retrieve-information-about-the-printer-driver-used-by-a-printer\/"},"modified":"2005-07-19T17:05:00","modified_gmt":"2005-07-19T17:05:00","slug":"how-can-i-retrieve-information-about-the-printer-driver-used-by-a-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-information-about-the-printer-driver-used-by-a-printer\/","title":{"rendered":"How Can I Retrieve Information About the Printer Driver Used by a Printer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I retrieve information about the printer driver used by a specific printer?<BR><BR>&#8212; RW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RW. Did you say something? Oh, right: printer drivers. You\u2019ll have to excuse us today: the Scripting Guys (well, one of them anyway) are in mourning. That\u2019s because the Colt League All-Star team he helped coached finished third in the Northwest Regional Tournament. What\u2019s wrong with finishing third in the Northwest region? Well, nothing\u2026except that the team that finished first gets to go to California and play in the Zone tournament, with the winner there going on to the Colt League World series. Everyone else &#8211; including the team that finished third &#8211; gets to go home. <\/P>\n<P>Anyway, thanks for asking: given time, we\u2019ll recover and get back to our usual rollicking selves. Try us again in a month or two. Or maybe next year sometime. Perhaps by 2008 we\u2019ll be ready to answer questions again. Until then\u2026.<\/P>\n<P>Um, never mind: our manager just informed us that we\u2019ve made a remarkable recovery and are ready to answer questions <I>now<\/I>. (Obviously he doesn\u2019t understand how important Colt League baseball is.) So we\u2019ll answer your question. And <I>then<\/I> we\u2019ll go back into mourning.<\/P>\n<P>Because this is a printing-related question we have the usual caveat: we can show you a solution that works great on Windows XP and Windows Server 2003, but won\u2019t work on Windows 2000 or any other version of Windows. On those other platforms you can use a script like this one to return the <I>name<\/I> of the print driver used by a specific printer, but that\u2019s pretty much all the information you can get:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where DeviceID=&#8217;\\\\\\\\atl-ps-1\\\\printer1&#8242; &#8220;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Name\n    Wscript.Echo objItem.DriverName\nNext\n<\/PRE>\n<P>Better than nothing, we suppose, but the name alone doesn\u2019t exactly qualify as \u201cdetails\u201d about the printer driver. If you want detailed information like that, you need to use a script like this one, a script that runs only on Windows XP or Windows Server 2003:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Associators Of {Win32_Printer.DeviceID=&#8217;\\\\atl-ps-01\\printer1&#8242;} &#8221; _\n        &amp; &#8220;WHERE AssocClass = Win32_DriverForDevice Role=Antecedent&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Name\n    Wscript.Echo objItem.DriverPath\nNext\n<\/PRE>\n<P>What we\u2019re doing here is taking advantage of two new classes that were added to Windows XP and Windows Server 2003. Although you don\u2019t see it explicitly mentioned in the script, we\u2019re using the new <B>Win32_PrinterDriver<\/B> class to retrieve information about the printer driver; the properties <B>Name<\/B> and <B>DriverPath<\/B> (both echoed in our For Each loop) are properties of the Win32_PrinterDriver class. <\/P>\n<P>The second class &#8211; which <I>is<\/I> explicitly spelled-out in the script &#8211; is the <B>Win32_DriverForDevice<\/B> class. This is an association class which, as the name implies, makes an association between two classes; in this case, Win32_DriverForDevice makes an association between a printer in the <B>Win32_Printer<\/B> class and a printer driver in the Win32_PrinterDriver class. To make this association we need to use an <B>Associators Of<\/B> query like this one:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Associators Of {Win32_Printer.DeviceID=&#8217;\\\\atl-ps-01\\printer1&#8242;} &#8221; _\n        &amp; &#8220;WHERE AssocClass = Win32_DriverForDevice Role=Antecedent&#8221;)\n<\/PRE>\n<P>In plain English (not exactly a Scripting Guys specialty!) this query says, \u201cFor all the printers with the DeviceID \\\\atl-ps-01\\printer1 use the Win32_DriverForDevice class to retrieve all the printer drivers associated with that printer.\u201d <\/P>\n<P>As usual, the query returns a collection, in this case a collection of all the printer drivers associated with the printer \\\\atl-ps-01\\printer1. In our For Each loop we iterate through the collection and echo back the Name and DriverPath properties. Problem solved.<\/P>\n<P>That much you could probably figure out for yourself; the query\u2019s a bit odd-looking, but fairly intuitive. Except maybe for one thing: what does <B>Role=Antecedent<\/B> mean? Well, in the wonderful world of WMI an antecedent object is the independent object in a relationship; in other words, you can have all kinds of print drivers, none of which are dependent on a particular printer. However, a printer can have only one print driver, which means the printer plays a <B>Dependent<\/B> role in the relationship. (Yes, it does sound like an episode of <I>Desperate Housewives<\/I>, doesn\u2019t it?) That means with a few tweaks we can flip this script inside-out and have it query the Win32_PrinterDriver class to return the names of all the printers that use a specified driver:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Associators Of {Win32_PrinterDriver.Name=&#8217;Xerox Document Centre 255 PS,3,Windows NT x86&#8242;} &#8221; _\n        &amp; &#8220;WHERE AssocClass = Win32_DriverForDevice Role=Dependent&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.DeviceID\nNext\n<\/PRE>\n<P>Give it a try and see what happens.<\/P>\n<P>Now, if you\u2019ll excuse us, we have\u2026work\u2026to do\u2026.<\/P>\n<P>(In case you\u2019re wondering, there\u2019s a reason why there aren\u2019t any books titled <I>The Third-Best and the Third-Brightest<\/I>: third-place isn\u2019t anywhere near as much fun as it might sound!)<\/P><BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I retrieve information about the printer driver used by a specific printer?&#8212; RW Hey, RW. Did you say something? Oh, right: printer drivers. You\u2019ll have to excuse us today: the Scripting Guys (well, one of them anyway) are in mourning. That\u2019s because the Colt League All-Star team he helped coached [&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-69363","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! How can I retrieve information about the printer driver used by a specific printer?&#8212; RW Hey, RW. Did you say something? Oh, right: printer drivers. You\u2019ll have to excuse us today: the Scripting Guys (well, one of them anyway) are in mourning. That\u2019s because the Colt League All-Star team he helped coached [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69363","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=69363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69363\/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=69363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}