How to Add Printers with PowerShell

Nobody likes to manually install printers. This is not fun. People who tell you otherwise are probably, in fact, lying, mistaken and must enjoy busy work.

Want to know something that’s definitely more fun? Hiking. Hiking is awesome. But also, installing printers remotely from the comfort of your workstation is awesome.

Now that we’ve established that, let’s move on to the fun!

(Windows 8.1 / Server 2012 R2 Users: those of you who are using either of these operating systems can follow this link for even more amazing cmdlets in Powershell that make printer management even more of a breeze!)

As for the rest of us, we’ll have to make due with what we’ve got.

Prerequisites:

  • A printer to install
  • Drivers for above printer

Here is the code. A lot of this may be self-explanatory, but I’m going to explain anyway:

    ####################################################
    # Change these values to the appropriate values in your environment

    $PrinterIP = "10.10.10.10"
    $PrinterPort = "9100"
    $PrinterPortName = "IP_" + $PrinterIP
    $DriverName = "KONICA MINOLTA bizhub C35P PS"
    $DriverPath = "\\UNC_Path\To\My\Drivers"
    $DriverInf = "\\UNC_Path\To\My\Drivers\KOBJQA__.inf"
    $PrinterCaption = "Konica Minolta C35P"
    ####################################################

    ### ComputerList Option 1 ###
    # $ComputerList = @("lana", "lisaburger")

    ### ComputerList Option 2 ###
    # $ComputerList = @()
    # Import-Csv "C:\Temp\ComputersThatNeedPrinters.csv" | `
    # % {$ComputerList += $_.Computer}

    Function CreatePrinterPort {
    param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
    $wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
    $wmi.psbase.scope.options.enablePrivileges = $true
    $Port = $wmi.createInstance()
    $Port.name = $PrinterPortName
    $Port.hostAddress = $PrinterIP
    $Port.portNumber = $PrinterPort
    $Port.SNMPEnabled = $false
    $Port.Protocol = 1
    $Port.put()
    }

    Function InstallPrinterDriver {
    Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
    $wmi = [wmiclass]"\\$ComputerName\Root\cimv2:Win32_PrinterDriver"
    $wmi.psbase.scope.options.enablePrivileges = $true
    $wmi.psbase.Scope.Options.Impersonation = `
     [System.Management.ImpersonationLevel]::Impersonate
    $Driver = $wmi.CreateInstance()
    $Driver.Name = $DriverName
    $Driver.DriverPath = $DriverPath
    $Driver.InfName = $DriverInf
    $wmi.AddPrinterDriver($Driver)
    $wmi.Put()
    }

    Function CreatePrinter {
    param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
    $wmi = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
    $Printer = $wmi.CreateInstance()
    $Printer.Caption = $PrinterCaption
    $Printer.DriverName = $DriverName
    $Printer.PortName = $PrinterPortName
    $Printer.DeviceID = $PrinterCaption
    $Printer.Put()
    }

    foreach ($computer in $ComputerList) {
     CreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort `
     -PrinterPortName $PrinterPortName -ComputerName $computer
     InstallPrinterDriver -DriverName $DriverName -DriverPath `
     $DriverPath -DriverInf $DriverInf -ComputerName $computer
     CreatePrinter -PrinterPortName $PrinterPortName -DriverName `
     $DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
    }
    ####################################################

PrinterIP – IP Address of the Printer.
PrinterPort – Port of the printer.
PrinterPortName – Name of the printer port. IP_ is standard for TCP/IP port
DriverName – This is the name of the printer as it appears in the *.inf file (case sensitive).
DriverPath – This is the directory where the printer’s install *.inf file is found.
DriverInf – Full path and file name of the *.inf file.
PrinterCaption – Name of printer as it will appear on the workstation.
ComputerList – Names of the computers you wish to install the printer on. Choose Option 1 or Option 2

(If you decide to use Option 2 to import from a .csv file to populate the computers, please change the highlighted word to the appropriate name of the header in your .csv file. My .csv file (ComputersThatNeedPrinters.csv) has a column header named “Computer”)

Import-Csv "C:\Software\Scripts\Powershell\ComputersThatNeedPrinters.csv" | % {$ComputerList += $_.Computer}

I’ve broken this script up into 3 functions: Creating the network printer port; installing the printer driver; creating the printer. I make use of WMI for each function (WMI Info). Feel free to use any or all of the functions in your designs.

That’s it!

I cannot guarantee that it will work in every environment, but it’s a pretty “cut and paste” script. If you decide to use it, please test it before rolling it out on any production workstations.

Busy work is monotonous, boring, and (most importantly) not fun. On the other hand, installing printers remotely is pretty fantastic, if you have the right tools.

Go forth and print!

A pastebin of the previous code can be found here.