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

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()}"

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