# How do I add a standard TCP/IP printer port from a command line?

Is there a way to use Rundll32.exe tcpmonui.dll,LocalAddPortUI from a command line to add a standard TCP/IP port for a printer?

Or any other way to add a standard TCP/IP printer port from a command line.

-

Acording to this page you can use the prnport command in XP.

-

you mean like:

NET USE [local port to bind to ie LPT1] \\ComputerName\printer_share /PERSISTENT:YES


note:

• this works with ip addresses, not just URI's
• you can loop back a local printer NET USE LPT1: \\127.0.0.1:9100
-
He said TCP/IP printer port. You have provided instructions for adding an SMB printer. –  eleven81 Oct 27 '09 at 21:10
I think he needs to address it by IP, not server share. In my brief googling I could not find a way to do it unless the TCP/IP port had already been created locally. –  djhowell Oct 27 '09 at 21:23
that will accept '\\172.168.1.10' as an argument. LocalAddPortUI will force a dialog. –  Greg Buehler Oct 27 '09 at 21:29

Note The following sample syntax works correctly as long as the standard TCP/IP ports are created:

rundll32 printui.dll,PrintUIEntry /if /b "Test Printer" /f %windir%\inf\ntprint.inf /r  "IP_157.57.50.98" /m "HP Laserjet 4000 Series PCL" /Z


If you do not enter the printer name correctly, or if you specify a printer that is not connected to the server, standard TCP/IP ports are not created, and you may receive the following error message...

-

Just so that others can find this here's the exact command (from cmd.exe) that I run to programmatically add a network printer under Windows 7 (this seems to be somewhat rare even after a lot of googling):

cscript c:\Windows\System32\Printing_Admin_Scripts\en-US\prnport.vbs ^
-a -r IP_10.0.0.100 -h 10.0.0.100

rundll32 printui.dll,PrintUIEntry /if /b "printer" /f %windir%\inf\prnhp002.inf^
/r "IP_10.0.0.100" /m "HP LaserJet 4200/4300 PCL6" /Z


(if you didn't know, as I didn't, ^ is the line continuation character in cmd.exe). You'll probably have to use a different inf file than the one listed above. I grep through all files in C:\Windows\inf looking for the exact text of my printer driver (HP LaserJet 4200/4300 PCL6") to discover the one that I need.

Another useful trick is to run a PowerShell one-liner to clear out any old printers that may exist (if it isn't obvious, this will delete all printers that you currently have installed):

powershell -command "foreach ($p in Get-WmiObject Win32_Printer) {$p.Delete()}"

-