{"id":68143,"date":"2006-01-19T10:37:00","date_gmt":"2006-01-19T10:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/19\/how-can-i-tell-which-users-are-connected-to-a-print-queue\/"},"modified":"2006-01-19T10:37:00","modified_gmt":"2006-01-19T10:37:00","slug":"how-can-i-tell-which-users-are-connected-to-a-print-queue","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-which-users-are-connected-to-a-print-queue\/","title":{"rendered":"How Can I Tell Which Users are Connected to a Print Queue?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I tell which users are connected to a print queue?<BR><BR>&#8212; RE<\/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, RE. You know, if your printers are anything like the ones the Scripting Guys are used to working with then it\u2019s easy to tell which users are connected to a print queue: all you have to do is walk into the printer room and see who\u2019s in there frantically looking in every nook and cranny for the document that the printer <I>insists<\/I> it printed. At Microsoft the paperless office is very close to a reality. Not because we consciously set out to save paper, mind you, but because you typically have a better chance of hitting the jackpot in Las Vegas than you do of getting a document to actually print. <\/P>\n<P>If you have printers that are a tad bit more reliable than that, however, you might find it easier to use a script to determine which users are currently printing to a particular printer. There\u2019s a bit of a trick to doing this, and you have to use a slightly different approach to carry out this task on any version of Windows prior to Windows XP. But we\u2019ll explain all that in a moment. For now, let\u2019s take a look at a script that works on Windows XP and Windows Server 2003:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrintJobs =  objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PrintJob Where Name Like &#8216;%FinancePrinter%'&#8221;)<\/p>\n<p>For Each objPrintJob in colPrintJobs \n    Wscript.Echo objPrintJob.Owner\nNext\n<\/PRE>\n<P>As you can see, there\u2019s not much to the script. It begins by connecting to the WMI service on the local computer (although the script could just as easily run against a remote machine). Here\u2019s where it gets a little tricky.<\/P>\n<P>As it turns out, the WMI class <B>Win32_PrintJob<\/B> can return the logon name of all the users currently printing to <I>all<\/I> the printers connected to a computer. Interestingly enough, however, Win32_PrintJob does not have a class that pinpoints <I>which<\/I> print queue a user is connected to. At best, you can identify the printer using the <B>Name<\/B> property. Values for this property are composed of two items: the name of the printer and a print job ID. For example, the print jobs currently in the queue for the printer FinancePrinter might look like this:<\/P><PRE class=\"codeSample\">FinancePrinter, 1\nFinancePrinter, 2\nFinancePrinter, 3\n<\/PRE>\n<P>Is that a problem? Yes, it is; after all, it prevents us from using a query like this to return all the print jobs in the FinancePrinter print queue:<\/P><PRE class=\"codeSample\">Set colPrintJobs =  objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PrintJob Where Name = &#8216;FinancePrinter'&#8221;)\n<\/PRE>\n<P>Why can\u2019t we use this query? Well, because there won\u2019t <I>be<\/I> any print jobs with the Name FinancePrinter; instead, all the print jobs will be named things like <I>FinancePrinter, 1<\/I>. How \u2026 delightful \u2026.<\/P>\n<P>But that\u2019s all right; as usual, we can work around WMI\u2019s idiosyncrasies. On Windows XP and Windows Server 2003 we can use the LIKE operator and a wildcard query, one that looks like this:<\/P><PRE class=\"codeSample\">Set colPrintJobs =  objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PrintJob Where Name Like &#8216;%FinancePrinter%'&#8221;)\n<\/PRE>\n<P>Note the construction of our Where clause: <B>Where Name Like \u2018%FinancePrinter%\u2019<\/B>. The percent signs are our wildcard characters: they signify any character or set of characters, much the same way the asterisk does when used with the dir command. The command <B>dir *.*<\/B> means, \u201cShow me all the files and folders whose names start with any character or set of characters, have a period in it, then end with any character or set of characters.\u201d Our WQL query can be read in similar fashion: \u201cBring back all the print jobs where the Name property starts with any character or set of characters, has the term <I>FinancePrinter<\/I> in it, then ends with any character or set of characters.\u201d (And, yes, \u201cany character or set of characters\u201d also means no character at all.) In other words, bring back any print job where the Name includes the string <I>FinancePrinter<\/I>.<\/P>\n<P>At that point all we have to do is set up a For Each loop and walk through the collection of print jobs, echoing back the value of the <B>Owner<\/B> property for each one. The Owner property is the logon name of the connected user; for example, <B>kmyer<\/B> or <B>packerman<\/B>. That\u2019s the best you can do with Win32_PrintJob; if you need, say, the user\u2019s first and last name, you\u2019ll have to do something else. (One suggestion: take the logon name [also known as the sAMAccountName] and use it in an Active Directory search that can return the user\u2019s first and last name. For more information, see the <I>Tales from the Script<\/I> column <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude, Where\u2019s My Printer?<\/B><\/A>)<\/P>\n<P>Now that\u2019s all well and good on Windows XP and Windows Server 2003; unfortunately, this script won\u2019t do much for anyone running an earlier version of Windows. That\u2019s because the LIKE operator isn\u2019t available on earlier version of Windows. That doesn\u2019t mean you\u2019re out of luck, it just means we have to do things the old-fashioned way. For example, instead of writing a query that returns only the print jobs being printed to FinancePrinter we need to write a query that returns <I>all<\/I> the print jobs, regardless of printer:<\/P><PRE class=\"codeSample\">Set colPrintJobs =  objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PrintJob&#8221;)\n<\/PRE>\n<P>Once we have the entire collection of print jobs we can then use the <B>InStr<\/B> function to determine whether or not the Name property includes the string <I>FinancePrinter<\/I>:<\/P><PRE class=\"codeSample\">If Instr(objPrintJob.Name, &#8220;FinancePrinter&#8221;) Then\n<\/PRE>\n<P>It\u2019s a little more cumbersome, but it\u2019ll work. Here\u2019s a complete script that returns the list of users currently connected to a Windows 2000 print queue:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrintJobs =  objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PrintJob&#8221;)<\/p>\n<p>For Each objPrintJob in colPrintJobs \n    If Instr(objPrintJob.Name, &#8220;FinancePrinter&#8221;) Then\n        Wscript.Echo objPrintJob.Owner\n    End If\nNext\n<\/PRE>\n<P>Unfortunately, we don\u2019t have a script that can tell you where printed documents actually go when the printer <I>says<\/I> they were printed yet the printed pages are nowhere to be found. But we\u2019re working on it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell which users are connected to a print queue?&#8212; RE Hey, RE. You know, if your printers are anything like the ones the Scripting Guys are used to working with then it\u2019s easy to tell which users are connected to a print queue: all you have to do is [&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":[712,523,404,713,3,4,5,6],"class_list":["post-68143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-and-jobs","tag-print-servers","tag-printing","tag-queues","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell which users are connected to a print queue?&#8212; RE Hey, RE. You know, if your printers are anything like the ones the Scripting Guys are used to working with then it\u2019s easy to tell which users are connected to a print queue: all you have to do is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68143","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=68143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68143\/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=68143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}