bulk create tcpipprinterport

by rthilo at 2012-09-27 10:23:55


I found this code at http://www.powershellcommunity.org, but it doesn’t seem to work:

$server = “localhost”
$port = ([WMICLASS]"\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()

currently testing on Win7-64 Enterprise, PowerShell opened “run as admin”

$port.Put() doesn’t report any error (no output)
$port correctly presets, what SHOULD have been created. But looking in the settings of my printers, the port doesn’t show up.

What do I miss?
by jtl at 2012-09-27 12:30:03
Is the port not even created for your default printer? This works for me on Win 7 x64 Enterprise but only for my default printer. Maybe use the SetDefaultPrinter() method for each value returned from Win32_Printer and execute the above code in a foreach loop?
by rthilo at 2012-09-28 00:33:32

Thanks for your reply.

Q: Is the port not even created for your default printer?
A: No. Not for ANY of the locally installed printers

What I found so far is, if I create a IP printer port for ANY printer, this port will be available for ALL other printers as well. It seems to by a print server related thing.

Using Win7-x64-Enterprise here as well.

Regards, Rudi.
by poshoholic at 2012-09-28 07:41:45
I’m copying this thread to the WMI forum since it’s a WMI-focused issue.
by rthilo at 2012-10-01 00:05:36
OK, thanks for copying this thread.

Hoping that someone will jump in to help me to solve this issue.
Is there something like a “WMI debugging feature”, that’s able to log, what call finally was passed to WMI, if it was accepted, and what the WMI system “thinks” it has done? (some kind of WMI - ProMon.exe)

Regards, Rudi.
by coderaven at 2012-10-03 05:23:33
If you bulk create TCPIPPrinterPorts, they are just ports, not printers. If you go to any printer properties on on your system and look at the ports tab you see all the ports you have available and can assign that port to any printer or more than one printer. This would only appear for local printers not remote printers.

Assuming we are past the above statement, instead of looking at printer properties, I would look at the Print Management admin console and make sure you have a connection to your local system and check ports.

I have used the same code you have in the past on many occasions and I think, your Put statement should output a result telling you what was done. Make sure you do not have your $ErrorActionPreference set to SilentlyContinue.
by rthilo at 2012-10-04 01:37:22


It’s now working, with exact the same lines. Most propably I missed to start PowerShell “run as Administrator” (if NOT done so, it fails).

The final $port.Put() doesn’t report success / failure or reason of failure (the box without “run as Administrator”), so again the question, are there debugging options for WMI actions, so that I would see in this case, that missing rights were the issue?

Thanks for your reply, Rudi.
by robertmcdonnell at 2012-10-25 12:43:19
I wrote some Printer modules based off of Richard’s WMI book. Here is the New-PrinterPort CMDlet that I use. It needs alittle work but it’s totally functional. Let me know if it’s of any use:

#Requires -Version 2.0
function New-PrinterPort{
Create a TCPIP printer port on a remote or local computer

Creates a new TCPIP printer port

New-PrinterPort -Name -HostAddress

Creates a printer port on the local computer using the name and address
This port will use the following default values:

Protocol: 1 (RAW)
PortNumber: 9100
SNMP: Disabled

New-PrinterPort -Name IP_1.2.3.4 -HostAddress -Protocol 1 -PortName 9100 -SNMPEnabled $false -Computername SERVERNAME

Creates a printer port on the remote computer SERVERNAME using the name IP_1.2.3.4 and the IP address
This port will be configured to use RAW port 9100 with SNMP disabled.

The name of the TCPIP printer port

.PARAMETER HostAddress
The IP address of the TCPIP printer port

The protocol used for the printer port (1 = RAW, 2 = LPR)
Default is 1

The port number for the printer port
Default is 9100

Specifies whether SNMP is enabled (True) or disabled (False)
Default is Disabled

.PARAMETER ComputerName
The host on which you would like to create the TCPIP printer port.

NAME: New-PrinterPort
AUTHOR: robertmcdonnell
KEYWORDS: New-PrinterPort


param (
[string]$ComputerName = “.”,

[parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Mandatory = $true)]

[bool]$SNMPEnabled = $false,

[parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[int]$Protocol = 1,

[parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[int]$PortNumber = 9100,

[parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Mandatory = $true)]
$port = ([WMICLASS]"\$ComputerName\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()
$port.Name = $Name
$port.HostAddress = $HostAddress
$port.Protocol = $Protocol
$port.PortNumber = $PortNumber
$port.SNMPEnabled = $SNMPEnabled