In the previous article, we looked at the old-school VBS scripts that can be used to manage printers and print operations in all Windows versions, starting from Windows XP. Today we are going to consider typical commands to install, manage and remove printers, print ports, drivers, and queues using PowerShell. These ways of managing printers from PowerShell CLI can be used in modern operating systems – Windows 10 / 8.1 and Windows Server 2019 / 2016 / 2012 R2.
PowerShell module: PrintManagement
Along with the release of Windows 8.1 and Windows Server 2012 R2, Microsoft released a new version of PowerShell 4.0 (a part of Windows Management Framework 4.0), which significantly extended the list of the Windows-based print server management cmdlets. You can get the full list of print, driver and print queue management cmdlets available in the PrintManagement module on Windows 10 (PowerShell v5) with the following command:
Get-Command –Module PrintManagement
The PrintManagement module include 22 PowerShell cmdlets for managing printers, drivers, print ports, and queues:
- Add-Printer – add (install) new printer;
- Add-PrinterDriver – install new print driver;
- Add-PrinterPort – create local print port;
- Get-PrintConfiguration – display printer configuration;
- Get-Printer – display the list of printers installed on the computer;
- Get-PrinterDriver – display the list of the installed drivers;
- Get-PrinterPort – displays the list of the printer ports;
- Get-PrinterProperty – show printer properties;
- Get-PrintJob – get a list of printer print jobs;
- Read-PrinterNfcTag – get printer information from the NFC tag;
- Remove-Printer – remove the printer;
- Remove-PrinterDriver — remove the printer driver;
- Remove-PrinterPort – remove the printer port;
- Remove-PrintJob – delete a print job on the printer;
- Rename-Printer – rename the printer;
- Restart-PrintJob – restart the print job;
- Resume-PrintJob – resume the paused print job;
- Set-PrintConfiguration – set the printer configuration;
- Set-Printer – update the printer configuration;
- Set-PrinterProperty – change printer properties;
- Suspend-PrintJob – suspend (pause) the print job;
- Write-PrinterNfcTag – write 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 using commands:
Get-Help < cmdlet_name> -Examples
Let’s look at a few examples of typical printer management tasks using PowerShell in Windows 10.
Adding Printer Drivers to the DriverStore
To list the print drivers that are installed in the Windows DriverStore:
Get-PrinterDriver
Then, install a new printer driver in the system. For example, you want to install the popular print driver “HP Universal Printing PCL 6”. According to the documentation, the PowerShell command to add a print driver should be as follows:
Add-PrinterDriver -Name "HP Universal Printing PCL 6" -InfPath "C:\Distr\HP-pcl6-x64\hpcu118u.inf"
However, when trying to install a driver in this way, the following error message appears:
Add-PrinterDriver : One or more specified parameters for this operation has an invalid value.At line:1 char:1+ Add-PrinterDriver -Name “HP Universal Printing PCL 6” -InfPath “C:\Di …+ ~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (MSFT_PrinterDriver:ROOT/StandardCimv2/MSFT_PrinterDriver) [Add-PrinterDriver], CimException + 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 print driver that is not in the Driver Store using Add-PrinterDriver command. To add a driver to the DriverStore, you can use:
- the VBS script described in the previous article;
- the utility — pnputil.exe. The command can looks as follow:
pnputil.exe -i -a C:\Distr\HP-pcl6-x64\hpcu118u.inf
(installs the specific printer driver) orpnputil.exe -i -a C:\Distr\HP-pcl6-x64\*.inf
(installs all the drivers found in the INF files in the specified directory); - the cmdlet Add-WindowsDriver that allows to integrate drivers into the offline Windows image.
After adding a printer driver to the driver repository, you should install it on the print server:
Add-PrinterDriver -Name "HP Universal Printing PCL 6"
How to Install Printer Using PowerShell?
Create an IP port for a network printer (here you can specify both the IP address of the network printer and the name of the remote print server):
Add-PrinterPort -Name "IP_192.168.10.26" -PrinterHostAddress "192.168.10.26"
Before adding a new IP print port, you can check if it exists:
$portName = "IP_192.168.10.26"
$checkPortExists = Get-Printerport -Name $portname -ErrorAction SilentlyContinue
if (-not $checkPortExists) {
Add-PrinterPort -name $portName -PrinterHostAddress "192.168.10.26"
}
With the help of the following command, we will install and share a new printer on the computer:
Add-Printer -Name hp3027_Office1_Buh -DriverName "HP LaserJet M3027 MFP PCL6 Class Driver" -PortName IP_192.168.10.26 -Shared -ShareName "hp3027_1_BUh" –Published
After running these commands, a new shared printer with the name “hp3027_Office1” will appear in the system.
To rename the printer, just run the command:
Rename-Printer -Name "hp3027_1_Buh" -NewName "hp3027_F1_Salary"
List Installed Printers on a Print Server
Let’s display the full list of printers installed on this computer:
Get-Printer
As you can see, the command shows the printer name, type (local or network), driver, print port, whether the printer is shared and published in the Active Directory.
Most PrintManagement cmdlets can be used to view status and manage printers, drivers and print queues on remote computers (print servers). The name of the remote computer or server is specified as an argument of the –ComputerName parameter.
You can get information about installed printers on a remote computer using PowerShell command:
Get-Printer -ComputerName rome-prnt1 | Format-List Name,DriverName
To display only a list of shared printers, use the command:
Get-Printer -ComputerName rome-prnt1 | where Shared -eq $true | fl Name
Connecting to a Network Shared Printer with PowerShell
To connect the shared printer from the print server, use the command:
Add-Printer -ConnectionName \\rome-prnt1\HP3027
Windows 10 uses the latest printer that was used for printing as the default printer. If you want to use a fixed default printer, run the command:
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 –Force
To set the default printer, you can use the following commands:
$wsnObj = New-Object -COM WScript.Network
$wsnObj.SetDefaultPrinter(%PrinterName%)
How to Remove a Printer Using PowerShell?
To remove a printer, you need to run the following PowerShell command:
Remove-Printer -Name "hp3027_L1_O1"
You can remove a specific driver using the Remove-PrinterDriver cmdlet:
Remove-PrinterDriver -Name "HP Universal Printing PCL 6"