Using Powershell to Administer Printers

by Brandon Dillinger

Have you ever needed to restart the print spooler service, check the print queue, and verify proper settings for a desktop printer on a remote workstation in a Microsoft Windows environment? If you said yes and you’re not using powershell to do these tasks then follow along while we explore just a few of the awesome administrative tasks that powershell is capable of. Get-WMIObject (or gwmi for short) is how you interact with WMI from within Powershell. WMI (Windows Management Instrumentation) has instructions for a wide range of administrative tasks. Today we will be talking about printer management on remote machines, with a little service management thrown in for good measure. First things first, you need to open powershell, so go ahead and press Ctrl + R to bring up a run dialog, type in powershell and hit enter. You should be presented with a screen that looks something like the following.

Image

Powershell accepts all DOS commands as well as it’s own scripting language. Lets start by looking at printers locally. Type Get-WMIObject win32_printer to get a list of printers installed on your computer. As you can see I only have the Microsoft Office printers installed on my computer so far.

Image If you only wanted the list of printers installed you could pipe the results from your WMI query to win32_printer to Select-Object Name like so:

Get-WmiObject win32_printer -ComputerName WorkstationName | Select-Object Name

If you have a job stuck in a print queue you can use win32_printjob to see what all is printing, like so.

Get-WmiObject win32_printjob -ComputerName WorkstationName

If you have jobs stuck in the queue that you need to clear out, simply pipe them all to the delete method of win32_printjob.

Get-WmiObject win32_printjob -ComputerName WorkstationName | Foreach-Object { $_.delete() }

What if the spooler is the problem you might ask? There is a WMI function for service management as well, or you could use powershells built in service management. The WMI way:

Get-WmiObject win32_service -ComputerName WorkstationName | Where  $_.name -match 'spooler' }

The built in way:

Get-Service Spooler -ComputerName WorkstationName

Either way you want to go about it is fine. I prefer the WMI method as it usually has more methods and properties as shows in the following Get-Member screenshots.

Get-Service | Get-Member

Powershell


Get-WmiObject win32_service | Get-Member

Powershell     You could put together a script that clears the print queue of every printer and then restarts the print spooler service pretty easily. Something along the lines of the following would work.


$strComputer = Read-Host "What is the name of the computer?"
Get-WmiObject win32_printjob -cn $strComputer | ForEach-Object { $_.delete() }
$service = get-service -name spooler -ComputerName $strComputer
Restart-Service -inputobj $service -force

I hope his has been a useful primer in printer management using powershell and WMI.

Additional reading from the Microsoft Developer Network:

WMI Reference

WMI Printing Classes

Win32_Service

Advertisements