In the previous article we have studied old school VBS scripts that can be used to manage printers and printing system in all Windows versions, starting from Windows XP. Today, we are going to consider typical commands and procedures of printer and printer driver management in Windows 8.1 / Server 2012 R2 using Powershell.
Microsoft released a new PowerShell version 4.0 simultaneously with the launch of Windows 8.1 and Windows Server 2012 R2. The new version is a part of Windows Management Framework 4.0, and has a significantly extended the list of Windows-based print server management cmdlets. You can get the full list of print, driver and print queue management cmdlets available in Posh v4 with the following command:
Get-Command –Module PrintManagement
22 cmdlets are available in Powershell:
- Add-Printer – installs a printer
- Add-PrinterDriver – installs a printer driver
- Add-PrinterPort – creates a printer port
- Get-PrintConfiguration – displays printer configuration
- Get-Printer – displays the list of the installed printers
- Get-PrinterDriver – displays the list of the installed drivers
- Get-PrinterPort – displays the list of the installed printer ports
- Get-PrinterProperty – shows printer properties
- Get-PrintJob – shows the list of print jobs of the printer
- Read-PrinterNfcTag – reads information about a printer from the NFC tag
- Remove-Printer – removes a printer
- Remove-PrinterDriver — removes a driver
- Remove-PrinterPort – removes a printer port
- Remove-PrintJob – removes a print job
- Rename-Printer – renames a printer
- Restart-PrintJob – restarts a print job
- Resume-PrintJob – resumes a paused print job
- Set-PrintConfiguration – sets printer configuration
- Set-Printer – updates the printer configuration
- Set-PrinterProperty – changes printer properties
- Suspend-PrintJob – suspends a print job
- Write-PrinterNfcTag – writes information into the NFC tag
To get detailed information about the syntax of any command, use the following command:
Get-Help <cmdlet_name > -Detailed
Examples of the commands:
Get-Help <cmdlet_name > -Examples
Let’s look at the typical examples of cmdlets used in printer management in Windows 8.
Let’s display the list of printer drivers in the system:
Then, install a new printer driver in the system, for example HP Universal Printing PCL 6. According to the documentation, the command to add a driver should be as follows:
Add-PrinterDriver -Name "HP Universal Printing PCL 6" -InfPath "C:\Install\HP\hpcu160u.inf"
However, when trying to install a driver this way, the following error message appears:
At line:1 char:1
+ Add-PrinterDriver -Name “HP Universal Printing PCL 6” -InfPath “C:\Install\HP\ …
+ CategoryInfo : InvalidArgument: (MSFT_PrinterDriver:ROOT/StandardCimv2/MSFT_PrinterDriver) [Add-Printer
+ FullyQualifiedErrorId : HRESULT 0x80070057,Add-PrinterDriver
It turns out that the driver from the INF file can only be installed if it already exists in the DriverStore. It appears that you can’t install a driver that doesn’t exist in the DriverStore using Add-PrinterDriver command. To add a driver to the DriverStore, you can use:
- a VBS script described in the previous article
- the utility — pnputil.exe. The format is as follows:
Pnputil –a pnputil.exe -i -a C:\Install\HP\hpcu160u.inf
(installs the specific printer driver) or
pnputil.exe -i -a C:\Install\HP\*.inf
(installs all the drivers found in the INF files in the specified directory)
- the cmdlet Add-WindowsDriver that allows to integrate drivers in the offline Windows image
After adding a printer driver to the DriverStore, you should install it on the printer server.
Add-PrinterDriver -Name "HP Universal Printing PCL 6"
Create an IP port to print using a network printer:
Add-PrinterPort -Name "IP_10.10.10.26" -PrinterHostAddress "10.10.10.26"
We create a new printer in the system and publish (i.e. share) it using the following command:
Add-Printer -Name hp3027_Office1 -DriverName "HP LaserJet M2027 MFP PCL6 Class Driver" -PortName IP_10.10.10.26 -Shared -ShareName "hp2027_1" –Published
After running these commands, a new shared printer with the name «hp3027_Office1»will appear in the system.
The printer can be removed with the command:
Remove-Printer -Name "hp2027_Office1"
Remove the driver as follows:
Remove-PrinterDriver -Name "HP Universal Printing PCL 6"