{"id":69433,"date":"2005-07-08T15:50:00","date_gmt":"2005-07-08T15:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/08\/how-can-i-use-a-script-to-rename-a-printer\/"},"modified":"2005-07-08T15:50:00","modified_gmt":"2005-07-08T15:50:00","slug":"how-can-i-use-a-script-to-rename-a-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-a-script-to-rename-a-printer\/","title":{"rendered":"How Can I Use a Script to Rename a Printer?"},"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 use a script to rename a printer?<BR><BR>&#8212; SB<\/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, SB. Well, if your printer is connected to a Windows 2000 or Windows NT 4.0 print server we have bad news for you: you <I>can\u2019t<\/I> rename a printer using a script, at least not using the technologies included in the operating system. (There might, or might not, be third-party tools that enable you to do this on those older versions of Windows.) If your printer is connected to a computer running Windows XP or Windows Server 2003, however, the news is much better.<\/P>\n<P>That\u2019s because the <B>Win32_Printer<\/B> class &#8211; which was extensively overhauled for Windows XP and Windows Server 2003 &#8211; includes a new method named <B>RenamePrinter<\/B>, a method which definitely lives up to its name. You want to rename a printer using a script? No problem:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where DeviceID = &#8216;HP'&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.RenamePrinter(&#8220;ArtDepartmentPrinter&#8221;)\nNext\n<\/PRE>\n<P>The script begins by connecting to the WMI service on the local computer. We then use this line of code to retrieve a collection of all the printers named HP that are installed on that computer:<\/P><PRE class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where DeviceID = &#8216;HP'&#8221;)\n<\/PRE>\n<P>Because printer names must be unique, the returned collection will consist of, at most, a single item. We set up a For Each loop to loop through the collection, and then use this line of code to rename the printer:<\/P><PRE class=\"codeSample\">objPrinter.RenamePrinter(&#8220;ArtDepartmentPrinter&#8221;)\n<\/PRE>\n<P>All we do here is call the RenamePrinter method, passing as the sole parameter the new name to be given to the printer. That\u2019s it; because we\u2019re calling a method rather than changing the value of a read\/write property we don\u2019t even need to use the Put_ method to save the changes.<\/P>\n<P>As long as you\u2019re changing the name of the printer you might want to change the share name as well. This can be done by changing the printer name, then connecting to the \u201cnew\u201d printer and modifying the <B>ShareName<\/B> property. Note that you can change the share name even if the printer isn\u2019t currently being shared.<\/P>\n<P>Here\u2019s a revised script that changes the printer name and then changes the share name:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where DeviceID = &#8216;HP'&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.RenamePrinter(&#8220;ArtDepartmentPrinter&#8221;)\nNext<\/p>\n<p>Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where DeviceID = &#8216;ArtDepartmentPrinter&#8217; &#8220;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.ShareName = &#8220;ArtDepartmentPrinter&#8221;\n    objPrinter.Put_\nNext\n<\/PRE>\n<P>As you can see, after we change the printer name we then use this line of code to re-connect to the printer, this time using the new name, ArtDepartmentPrinter:<\/P><PRE class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where DeviceID = &#8216;ArtDepartmentPrinter&#8217; &#8220;)\n<\/PRE>\n<P>We set up a For Each loop to loop through the collection of printers (again, there will be only one item in that collection). Inside that loop we change the value of the ShareName property and then call the <B>Put_<\/B> method to save those changes. (As we implied earlier, any time you change the value of a read\/write property you need to call the Put_ method in order to save those changes.) That gives us a printer with a new name, a new share name, and, no doubt, a whole new outlook on life.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use a script to rename a printer?&#8212; SB Hey, SB. Well, if your printer is connected to a Windows 2000 or Windows NT 4.0 print server we have bad news for you: you can\u2019t rename a printer using a script, at least not using the technologies included in the [&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-69433","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 use a script to rename a printer?&#8212; SB Hey, SB. Well, if your printer is connected to a Windows 2000 or Windows NT 4.0 print server we have bad news for you: you can\u2019t rename a printer using a script, at least not using the technologies included in the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69433","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=69433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69433\/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=69433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}