Install a Printerport and Printer with PowerShell

Source: https://lazyadmin.nl/powershell/install-a-printerport-and-printer-with-powershell/

Last year I have written about how you can install a printer and printerport from the command line. Really useful if you want to create a batch script so users can install printers them self. But today I wanted to create some deployment scripts to do deploy some printers. And that made me thought, why am I not using PowerShell for this…?

Let me first explain why I am using a script to install printers and not a GPO. We have clients that go to remote standalone sites with there laptop. And they want to install the printer from that location of course. I am trying to keep the number of installed printers on a client as low as possible, so they don’t have to choose between 20 printers (resulting in a lot of misprints). So with PowerShell, we can add the printerport and printer on the client without the need of admin credentials.

Add PrinterPort

The first step is to create the printerport. We use the cmdlet add-printerport to create a TCP/IP port for our printer. The cmdlet is really simple to use,  just add a name and printer ip address to create a TCPIP printerport

Add-printerport -Name "TCPPort:192.168.0.200" -PrinterHostAddress "192.168.0.200"

With the command above you create a TCP print port name “TCPPort:192.168.0.200” with the Ip Address 192.168.0.200. Because we entered a PrinterHostAddress the cmdlet knows that you want a TCP port.

So you can name the printerport anything you want. To create a TCP Port you don’t need to add TCPPort: in front of it. But I recommend you do because this way you can see in the Printer Server > Ports view on the client which kind of ports are created. Also, the IP Address in the port name isn’t necessary. This will work to:

Add-printerport -Name "AwesomePort" -PrinterHostAddress "192.168.0.200"

Checking if a PrinterPort Exists

Now before we are adding a printerport with PowerShell it is always a good practice to check it port already exists. This way a prevent any error when running the script. To get the available ports you can use the Get-PrinterPort cmdlet. This will return a list of all available printer ports. By adding the -name switch you can specify the printerport your want to get.

So what we can do is get the printerport and if it does not exist we create it. You need to add the ErrorAction SilentlyContinue switch to suppress the error message you get when you request a non-existing printer port.

$portName = "TCPPort:192.168.0.200"

$portExists = Get-Printerport -Name $portname -ErrorAction SilentlyContinue

if (-not $portExists) {
  Add-PrinterPort -name $portName -PrinterHostAddress "192.168.0.200"
}

Adding the printer with PowerShell

Before we can add the printer we need to make sure the printer driver is installed on the machine. I deploy some generic print drivers when installing the computers, so they always have the correct driver available. If you need to install the driver to, then you can use PowerShell for that with the add-printerdriver cmdlet, but you will need Administrator credentials for this.

So before we are going to add the printer, we check if the printer driver exists. If we have a printer driver, then add the printer.

$printDriverName = "Canon iR-ADV C2020i/C2030i Class Driver"

$printDriverExists = Get-PrinterDriver -name $printDriverName -ErrorAction SilentlyContinue

if ($printDriverExists) {
    Add-Printer -Name "Canon Test Printer" -PortName $portName -DriverName $printDriverName
}else{
    Write-Warning "Printer Driver not installed"
}

Complete script

Below you will find the complete script for adding a printerport and printer trough PowerShell. You can polish this up by adding cmdlet bindings to you can run it from the command line or by including the driver installation as well.

$portName = "TCPPort:192.168.0.200"
$printDriverName = "Canon iR-ADV C2020i/C2030i Class Driver"


$portExists = Get-Printerport -Name $portname -ErrorAction SilentlyContinue

if (-not $portExists) {
  Add-PrinterPort -name $portName -PrinterHostAddress "192.168.0.200"
}

$printDriverExists = Get-PrinterDriver -name $printDriverName -ErrorAction SilentlyContinue

if ($printDriverExists) {
    Add-Printer -Name "Canon Test Printer" -PortName $portName -DriverName $printDriverName
}else{
    Write-Warning "Printer Driver not installed"
}
Close Menu