How Can I Use a Script to Rename a Printer?

How Can I Use a Script to Rename a Printer?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I use a script to rename a printer?

-- SB

SpacerHey, Scripting Guy! AnswerScript Center

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’t 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.

That’s because the Win32_Printer class - which was extensively overhauled for Windows XP and Windows Server 2003 - includes a new method named RenamePrinter, a method which definitely lives up to its name. You want to rename a printer using a script? No problem:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID = 'HP'")

For Each objPrinter in colPrinters
    objPrinter.RenamePrinter("ArtDepartmentPrinter")
Next

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:

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID = 'HP'")

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:

objPrinter.RenamePrinter("ArtDepartmentPrinter")

All we do here is call the RenamePrinter method, passing as the sole parameter the new name to be given to the printer. That’s it; because we’re calling a method rather than changing the value of a read/write property we don’t even need to use the Put_ method to save the changes.

As long as you’re 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 “new” printer and modifying the ShareName property. Note that you can change the share name even if the printer isn’t currently being shared.

Here’s a revised script that changes the printer name and then changes the share name:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters =  objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID = 'HP'")

For Each objPrinter in colPrinters
    objPrinter.RenamePrinter("ArtDepartmentPrinter")
Next

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID = 'ArtDepartmentPrinter' ")

For Each objPrinter in colPrinters
    objPrinter.ShareName = "ArtDepartmentPrinter"
    objPrinter.Put_
Next

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:

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID = 'ArtDepartmentPrinter' ")

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 Put_ 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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I can I use this script with a variable print name?

    the printer I want change it name is an RDP printer and the name changing by the session

    exmple:

    Samsung ML-3050 Series PCL 6 (from BS) in session 4

    the session changing

    sory abuot the English...