managing printers with PowerShell instead of VBScript?

Sometimes it's necessary to add and remove specific printers to a computer. For example during a client deployment or when a user logs on. This post covers how to manage printers with PowerShell.

The following PowerShell commands are supported with PowerShell version 4 and newer.

Installing a local network printer

Installing a local printer (without a printserver) consists of the following steps:

  1. Add the printer driver to your system's driverstore
  2. Install the printer driver from the driverstore
  3. Add a printer port to communicate with the printer
  4. Last but not least add the printer

Add the printer driver to the driverstore

Before you can install the printer driver you need to import the printer driver to your system's driverstore.

This can be achieved with the built in Windows "pnputil" utility.

The following code adds all drivers from the specified path to the driverstore:

Get-ChildItem %PathToYourDriverFolder% -Filter *.inf -Recurse | % {pnputil.exe /a $_.FullName}

Install the printer driver from the driverstore

This step is quite simple, you just need to know the name of the printer driver you want to install. For example "HP Universal Printing PCL 6".

Hint: To get the name of a driver you can check the "[strings]" section of your *.inf file.

Add-PrinterDriver -Name %DriverName% -Verbose

Add a printer port to communicate with the printer

As a best practise i recommend to use the printer ip address or hostname as port name.

Add-PrinterPort -Name %NameForYourPort% -PrinterHostAddress %PrinterIpAddress% -Verbose

Add the printer

Finally add the printer with the created port and driver and the specified name

Add-Printer -PortName %NameForYourPort% -Name %PrinterName% -DriverName %DriverName%

Installing a printer from a printserver

Installing a printer from a printserver is quite simple. You just need the hostname or ip address of the printserver and the shared name for the printer.

Add-Printer -ConnectionName \\%PrintServer%\%PrinterSharedName%

Setting a default printer

To set a default printer the printer must already be installed to your machine.

$wsObject = New-Object -COM WScript.Network $wsObject.SetDefaultPrinter(%PrinterName%)

Windows 10 uses by default the last chosen printer as default

If you need to specify a persistent default printer you can disable this feature with the following registry key:

Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Force

Thanks for reading my very first blog post. Hopefully you are able to start "managing printers the PowerShell way". You can find more information on the About section of my blog. Stay tuned.

Reference: