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"
}

Add Printerport with powershell

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"
}

 

Get more stuff like this

IT, Office365, Smart Home, PowerShell and Blogging Tips

I hate spam to, so you can unsubscribe at any time.

14 thoughts on “Install a Printerport and Printer with PowerShell”

  1. Rudy,
    thanks for providing the script. For me it was a good start-point to bulk-add 50 printers on a WS2016 Server. I wrapped your code in a foreach loop, imported necessary variables from a csv and added one line of code to do a DHCP reservation, too.
    Now the bulk creation took maybe 5 minutes with all comments filled from the CSV and kept me away from the “pesky” Job – as Sainath said 🙂

  2. This is great. But, especially with the Canon MFPs, you’re only half done.

    Now you need to set default preferences like print or store and which mailbox to store in, Color or Black and White…

    How do you go about setting any of these driver configuration parameters remotely? Having to log onto the console of each and every workstation seems ridiculous, but I’ve not found any other way in years of searching.

  3. Beautiful article Ruud!

    Worked like a charm on Windows 2016 Server(s).
    Saved me time, to automate other ‘pesky’ things 😉

    Thank you.
    Cheers!

  4. This is great but what about if you need to install an Internet Port (IPP)?

    Can’t seem to find any clear reference to this anywhere….

  5. So what if I want to run this on different network locations? It works if the driver has been installed on the computer before, but I want it to work on any computer it is run on.

    • Then you will have to copy and install the printer driver as well in the same script. I used pnputil for this:

      pnputil.exe -a "\\domain.local\Contoso\PrinterDrivers\Canon21-75ps\CNS30HA64.INF"

Leave a Comment

0 Shares
Tweet
Pin
Share
Share