Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the printer port for a printer? For example how can I change it from FILE: to LPT1:?

-- HK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, HK. Despite the Scripting Guys reputation as sophisticated intellectuals - really? We don’t have a reputation as sophisticated intellectuals? Really? This is very distressing.

Anyway, what we were going to say is that, despite the reputation we don’t seem to have, we do watch a lot of television. Because of that we get to see plenty of those truth-in-advertising drug commercials, the ones where drug companies tout the wonders of their new miracle cure and then, in a quiet voiceover, note that, “Side effects may include dizziness, nausea, drowsiness, and headaches; might also turn you into a newt. Should not be taken by women who are pregnant, who may become pregnant, who know someone who knew someone who was pregnant, or whose mother was once pregnant. Should not be taken by men unless they want to become pregnant.”

We were reminded of these standard drug company disclaimers because we need to start this column off with a standard disclaimer of our own: “The following script can be used to change the printer port for a printer, provided the script is run on a Windows XP or Windows Server 2003 computer. This script will not work on Windows 2000, Windows NT 4.0, Windows 98, Windows 95, Windows 3.1, or Windows 3.0 computers.”

Windows Me? Well, if you ever find someone who actually uses Windows Me, they’re out of luck, too. Sorry.

In other words, you can change the printer port for a printer, but only on Windows XP and Windows Server 2003 computers. And here’s a sample script that will do it:

strComputer = "."

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

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

For Each objPrinter in colPrinters
    objPrinter.PortName = "LPT1:"
    objPrinter.Put_
Next

As you can see, we connect to the WMI service and then use the ExecQuery method to retrieve a collection of all the printers with a DeviceID equal to Art Department Printer. If you try this script on your own, make sure you use a Where clause that will limit the returned collection to a single printer; otherwise you run the risk of changing the printer port for all the printers on your computer. (And, yes, DeviceID is a good way to go because DeviceIDs must be unique on a computer.)

After we get back our collection we set up a For Each loop to walk through all the printers in that collection. (If we construct our query correctly, there will be just one item in the collection.) Inside the For Each loop we change the value of the PortName property, then call the Put_ method to write those changes to the printer object. That’s all it takes.

So the next time you - wait a second: who just said that this column can cause dizziness, nausea, drowsiness, and headaches? Hey, we’re supposed to be the funny ones around here.

Well, we did say supposed to be….