1

On Windows 7 / 8[.1], you can configure printer and port information via the command line as the administrator, for example:

cd /d %WINDIR%\System32\Printing_Admin_Scripts\en-US
prnmngr -s computer-01 -l

will list the printers attached to computer-01, using my logged-in credentials. This produces many lines of output, including the printer port. My goal is to find the Printer Port, assuming it's a TCP/IP port, then run the prnport command to disable SNMP Status on that port. Below is a sample output of two different printers, on being USB, the other being a TCP/IP printer:

Server name
Printer name JMC-KMC224-BW
Share name
Driver name KONICA MINOLTA C364SeriesPCL
Port name IP_192.168.0.11
Comment
Location
Print processor KOAYTJ_P
Data type RAW
Parameters
Attributes 2628
Priority 1
Default priority 0
Average pages per minute 0
Printer status Idle
Extended printer status Unknown
Detected error state Unknown
Extended detected error state Unknown

Server name
Printer name DYMO LabelWriter 400
Share name
Driver name DYMO LabelWriter 400
Port name USB001
Comment
Location
Print processor winprint
Data type RAW
Parameters
Attributes 2624
Priority 1
Default priority 0
Average pages per minute 0
Printer status Idle
Extended printer status Unknown
Detected error state Unknown
Extended detected error state Unknown

(Note: this is not an X-Y problem, the issue is known to the manufacturer and disabling SNMP is the known solution.)

How can I find the Port Name that happens to be a TCP/IP port, and put it to a variable that I can run against the prnport command?

2

One problem you'll have is that the port name is free text: it can be just about anything; it is not guaranteed to be the IP address. Unless you're sure that the people setting up the printers explicitly set the names to start with IP_, that is not guaranteed.

I would recommend using PowerShell if you are able; the Get-PrinterPort and Set-PrinterProperty or maybe Set-PrintConfiguration cmdlets (Win10 docs, Win8 docs, Win8.1 docs) look very useful. Unfortunately, they're only available on Windows 8 or Server 2012 and newer. I only have access to Windows 7 at the moment and am unable to test them.


Failing that, you can use prnport directly rather than going through prnmngr first. prnport -l will "list all TCP ports". If you use this in conjunction with a script similar to David's, you'll end up with the correct results:

NB: You may need to substitute prnport invocations with cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnport.vbs depending on your setup. Also, the region en-US can change.

for /f "tokens=1,2,*" %%a in ('prnport -s computer-01 -l ^| find "Port name"') do (
    prnport -t -s computer-01 -r "%%c" -md
)

Brief explanation:

  • This is an expanded for loop. See for /? for details.
  • We're looping through all Port name foo lines in the output and taking foo from them.
  • By specifying tokens=1,2,*, we tokenise the result string by spaces and set %%a to Port, %%b to name and %%c to everything after (that's what the asterisk means).

You'll probably want to pass the computer name in as another variable.

Also note that this does not handle cases where no TCP/IP printers exist. I assume they'll just do nothing since the loop body should never execute.


Another option is to use WMI directly, which is what the printer management scripts do under the hood.

  • This is what I built into my script. It will take in one variable, a computer name, then connect to it, grab the ports with your command, and unset the SNMP Status on each TCP/IP port. I added the -t switch, needed for making changes to an existing port. Thanks! – Canadian Luke Mar 7 '16 at 17:37
  • The links posted to the Powershell commands do not work anymore – Canadian Luke Jan 10 '18 at 20:42
  • @CanadianLuke Eh... MSDN's broken a lot of links lately. Looks like they broke the Win10 redirect specifically and selecting the Win8 version works. Or manually finding the Win10 pages. I'm too lazy to maintain per-cmdlet links so the printmanagement module landing page will have to do. – Bob Jan 11 '18 at 1:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.