Skip to main content

How to Add a Local Printer Port in Batch Script

Standard network printers often "Auto-Configure" their ports, but specialty hardware (like barcode labelers, serial thermal printers, or legacy industrial plotters) require a specific Local Port (like LPT1, COM1, or a custom Standard TCP/IP Port) to communicate. Manually adding these ports through the deeply buried "Print Server Properties" menu is time-consuming and prone to errors. A Batch script can use the printui.dll utility or a PowerShell bridge to programmatically create these digital bridges, ensuring that your specialty hardware is mapped and ready for use as soon as it's plugged in.

This guide will explain how to automate the creation of printer ports.

Method 1: The TCP/IP Port Creation (PowerShell Bridge)

Adding a modern network printer via IP address requires a "Standard TCP/IP Port." This is most reliably done via PowerShell.

Implementation Script

@echo off
set "PortName=IP_192.168.1.50"
set "Address=192.168.1.50"

echo [ACTION] Creating Standard TCP/IP Port for %Address%...

:: Check if the port already exists to avoid duplicate errors
powershell -NoProfile -Command ^
"if (Get-PrinterPort -Name '%PortName%' -ErrorAction SilentlyContinue) {" ^
" Write-Host '[INFO] Port %PortName% already exists. No action needed.'; exit 0" ^
"} else {" ^
" try {" ^
" Add-PrinterPort -Name '%PortName%' -PrinterHostAddress '%Address%';" ^
" Write-Host '[SUCCESS] Port %PortName% is now available for printer assignment.'" ^
" } catch { Write-Host '[ERROR]' $_.Exception.Message; exit 1 }" ^
"}"

if %errorlevel% neq 0 (
echo [ERROR] Port creation failed. Ensure you are running as ADMIN.
)

pause
warning

Administrative Rights. Creating system-level communication ports requires hardware-level permissions. You MUST run your script as an Administrator.

Method 2: Adding a Custom Local Port

If you need to create a custom local port (such as a file output path or a named pipe for specialty hardware), use the Add-PrinterPort cmdlet with the -Name parameter.

@echo off
set "PortName=COM3:"

echo [SETUP] Adding local port: %PortName%...

:: Check if the port already exists
powershell -NoProfile -Command ^
"if (Get-PrinterPort -Name '%PortName%' -ErrorAction SilentlyContinue) {" ^
" Write-Host '[INFO] Port %PortName% already exists.'; exit 0" ^
"} else {" ^
" try {" ^
" Add-PrinterPort -Name '%PortName%';" ^
" Write-Host '[SUCCESS] Local port %PortName% created.'" ^
" } catch { Write-Host '[ERROR]' $_.Exception.Message; exit 1 }" ^
"}"

if %errorlevel% neq 0 (
echo [ERROR] Port creation failed. Ensure you are running as ADMIN.
)

pause

Method 3: The "Port Verification" Audit

Before adding a port, or after creating one, verify the current port inventory on the system.

@echo off
echo [AUDIT] Listing all printer ports...
echo.

echo --- TCP/IP Ports ---
wmic path Win32_TCPIPPrinterPort get Name, HostAddress 2>nul

echo.
echo --- All Ports ---
powershell -NoProfile -Command "Get-PrinterPort | Format-Table Name, Description, PrinterHostAddress -AutoSize"

echo.
pause

How to Avoid Common Errors

Wrong Way: Using "net use" for non-parallel ports

Commands like net use LPT1: \\server\printer are for redirecting old DOS print calls. They do not create a permanent Windows Printer Port that can be selected in the "Ports" tab of a modern printer driver.

Correct Way: Use the PowerShell Add-PrinterPort (Method 1). This creates a real, persistent system port that modern Windows drivers can "Bind" to.

Problem: Port "In Use"

If you try to create a port with a name that already exists (e.g., IP_10.0.0.1), the script will fail.

Solution: Use Method 3 to check the list first, or use the built-in duplicate check shown in Methods 1 and 2.

Best Practices and Rules

1. Match the Name and IP

By convention, port names should include the IP address (e.g., IP_192.168.1.10). This makes it obvious to future administrators which printer port connects to which physical hardware.

2. Identify "LPR" vs "Raw"

Most modern office printers use the RAW protocol on Port 9100. If your specialty hardware (like an older mainframe printer) requires LPR, you must specify the -LprHostAddress and -LprQueueName parameters in your PowerShell command.

3. Log the Maintenance

When deploying custom ports to a fleet of specialty machines, log the successful creation. This provides a "Blueprint" of your custom hardware mappings. powershell -NoProfile -Command "Get-PrinterPort | Format-Table Name, PrinterHostAddress" >> port_deployment_log.txt

Conclusions

Adding printer ports via Batch script is a critical step for managing industrial, medical, or legacy hardware in a Windows environment.

By moving beyond manual GUI-based setup and utilizing automated port creation, you ensure that your complex peripheral infrastructure is standardized and reliable.

This professional level of configuration ensures that your specialty devices stay connected and functional, regardless of how often you update or replace your workstations.