Use PowerShell to Create New Printer Ports
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:
- Create the printer port.
- Install the printer driver.
- 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:
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.
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
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.
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.
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy