August 6th, 2013

Use PowerShell to Create New Printer Ports

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell 3.0 to create new printer ports in Windows 8.

Microsoft Scripting Guy, Ed Wilson, is here. One of the exciting things that is happening around the Scripting House is the appearance of new Windows PowerShell Saturday events. We have new events coming up in Atlanta, Singapore, and Charlotte. For information about these and other events, check out my site, Scripting Community. If you do not know what Windows PowerShell is, check out my blog post, Community: All about PowerShell Saturday.

To programmatically create a working printer, there are at least three steps:

  1. Create the printer port.
  2. Install the printer driver.
  3. Install the printer (by using the printer port and the printer driver).

Today I am talking about creating the printer port.

Using PowerShell to work with printer ports

Before I create anything, I like to know what I have going on with my computer. I can use the Get-PrinterPort function to list existing printer ports on my local computer:

Get-PrinterPort

I can also use this function to retrieve printer port information from a remote server running Windows Server 2008 and Windows PowerShell 3.0 as shown here:

Get-PrinterPort -ComputerName dc1

The commands and the output from the commands are shown in the following image.

Image of command output

Adding a new printer port

To add a new printer port, I use the Add-PrinterPort function in Windows 8 or Windows Server 2012. By using the Add-PrinterPort function, I can add a local printer port, a TCP printer port, or an LPR printer port.

Most of the time, if I am creating a local printer port, I want to print directly to a printer on the network. Doing this bypasses a print server. Therefore, in the case of large print jobs, I lose flexibility because my laptop must remain on to manage the large print job. But for short documents, it is fast. Also by printing directly to the printer, I can configure things the way that I want.

By using Windows PowerShell, it is easy to create a TCP printer port. I use the Add-PrinterPort function, create a name for the port (the name does not matter, but it is best to use something that makes sense in the printing context). The IP address of the printer itself becomes the value for the PrinterHostAddress parameter. Here is the command I used:

Add-PrinterPort -Name ‘HP_Direct:’ -PrinterHostAddress ‘192.168.1.88’

I do not need to specify a value for the port number unless the printer is configured to use a different value than the default. The Add-PrinterPort function has four parameter sets, and I use the third one to create a TCP printer port. Here are the optional parameters for this parameter set:

Add-PrinterPort [-Name] <String> [-PrinterHostAddress] <String> [-AsJob

 [<SwitchParameter>]] [-CimSession <CimSession>] [-ComputerName <String>]

 [-PortNumber <UInt32>] [-SNMP <UInt32>] [-SNMPCommunity <String>]

 [-ThrottleLimit <Int32>] [-Confirm [<SwitchParameter>]] [-WhatIf

 [<SwitchParameter>]] [<CommonParameters>]

I have SNMP turned off on my network, so I do not need to specify a community string or any of that stuff.

When I add a printer port, I do not need an elevated Windows PowerShell console. In Windows 8, I can do this without additional rights. In addition, Windows PowerShell does not make any checks. Therefore, if the IP address is wrong or inaccessible, no warnings generate. This is shown here:

Add-PrinterPort -Name ‘bogus:’ -PrinterHostAddress ‘10.10.10.10’

The command and the output from Get-PrinterPort are shown in the following image.

Image of command output

Deleting the printer port

When I create something via Windows PowerShell, I also always like to know how to clean it up. To delete a printer port, I use the Remove-PrinterPort function. The use of the Remove-PrinterPort function is shown here:

Get-PrinterPort | Where name -eq ‘bogus:’ | Remove-PrinterPort

The following image shows the command and its associated output.

Image of command output

That is all there is to using Windows PowerShell to create new printer ports. Printer Week will continue tomorrow when I will talk about installing printer drivers.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.