{"id":69383,"date":"2005-07-15T09:51:00","date_gmt":"2005-07-15T09:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/15\/how-can-i-share-all-the-local-printers-on-a-computer\/"},"modified":"2005-07-15T09:51:00","modified_gmt":"2005-07-15T09:51:00","slug":"how-can-i-share-all-the-local-printers-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-share-all-the-local-printers-on-a-computer\/","title":{"rendered":"How Can I Share All the Local Printers on a Computer?"},"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 write a script that will automatically share all the local printers on a computer?<BR><BR>&#8212; CD<\/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, CD. You know, when we redid the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/hsgarch.mspx\"><B>Hey, Scripting Guy! archive<\/B><\/A> we created a new category devoted exclusively to printing. When we started to fill out the archive pages, however, we discovered &#8211; much to our surprise &#8211; that we hadn\u2019t really answered that many printing-related questions. We <I>thought<\/I> we\u2019d been answering a bunch of printing questions, but obviously we\u2019d simply been hiding those questions in the closet and hoping that they\u2019d go away.<\/P>\n<P>Why have we been so reluctant to answer printing questions? After all, isn\u2019t it really easy to manage printers in Windows XP or Windows Server 2003?<\/P>\n<P>As a matter of fact, it is; the enhancements made to the WMI printing classes represent one of the real strengths of scripting in Windows XP and Windows 2003. The problem is that the vast majority of these enhancements represent capabilities not found in Windows 2000; to be honest, print management in Windows 2000 isn\u2019t anything to write home about. (Or to write daily scripting columns about.) The solutions to most printing questions are XP\/2003-specific: they simply don\u2019t apply to Windows 2000. Because we prefer to offer generic solutions that work on most Windows platforms, we kind of hurry past printing questions and hope that no one notices.<\/P>\n<P>But what the heck; we\u2019ll go ahead and answer this one. Just keep in mind that, if you\u2019re running Windows 2000, today\u2019s column doesn\u2019t apply to you. This is XP\/2003 only. And, yes, we <I>know<\/I> that\u2019s not fair. But that\u2019s just the way it is.<\/P>\n<P>Here\u2019s a script that will share out all the local printers on a computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colInstalledPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Network = FALSE&#8221;)<\/p>\n<p>i = 1<\/p>\n<p>For Each objPrinter in colInstalledPrinters\n    objPrinter.Shared = TRUE\n    objPrinter.ShareName = &#8220;Printer&#8221; &amp; i\n    objPrinter.Put_\n    i = i + 1\nNext\n<\/PRE>\n<P>The script begins by connecting to the WMI service, then issues this query to return a collection of all the local printers; that is a collection of all the printers that are connected to a local printer port:<\/P><PRE class=\"codeSample\">Set colInstalledPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Network = FALSE&#8221;)\n<\/PRE>\n<P>After issuing the query we set the value of a counter variable named i to 1; we\u2019ll use this variable later on to give each of the local printers a unique share name. We then set up a For Each loop to loop through our collection of local printers.<\/P>\n<P>And what do we do inside that loop? Well, for each printer we start off by setting the value of the <B>Shared<\/B> property to True; this will cause the printer to be shared. The <B>Shared<\/B> property, by the way, is a new addition to the <B>Win32_Printer<\/B> class; this property does not appear in the version of WMI found in Windows NT or Windows 2000, which explains why this script will not work on those platforms.<\/P>\n<P>Because each shared printer requires a unique share name, we use this line of code to set the value of the <B>ShareName<\/B> property to the word <I>Printer<\/I> plus the value of our counter variable:<\/P><PRE class=\"codeSample\">objPrinter.ShareName = &#8220;Printer&#8221; &amp; i\n<\/PRE>\n<P>The first time through the loop i is equal to 1, hence the ShareName will be set to <I>Printer1<\/I>. We then call the <B>Put_<\/B> method to actually write the changes to the printer. Don\u2019t leave out this line of code; if you do, your changes won\u2019t be saved, and the printer will not be shared. We then increment the value of i by 1, and loop around again. That means the next printer in the collection will have the share name <I>Printer2<\/I>, the one after that will have the share name <I>Printer3<\/I>, and so on.<\/P>\n<P>Like we said, this script works great, provided you\u2019re running Windows XP or Windows Server 2003. If you\u2019re running some other version of Windows &#8211; oh, look we\u2019re out of time for today. Gotta go; bye!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write a script that will automatically share all the local printers on a computer?&#8212; CD Hey, CD. You know, when we redid the Hey, Scripting Guy! archive we created a new category devoted exclusively to printing. When we started to fill out the archive pages, however, we discovered &#8211; [&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,6],"class_list":["post-69383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I write a script that will automatically share all the local printers on a computer?&#8212; CD Hey, CD. You know, when we redid the Hey, Scripting Guy! archive we created a new category devoted exclusively to printing. When we started to fill out the archive pages, however, we discovered &#8211; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69383","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=69383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69383\/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=69383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}