Ezine 167 – PowerShell for Printers
From the dawn of computing, printers have given more problems than any other hardware. PowerShell is not the magic bullet to cure all your printer ills, but using PowerShell makes it easier to interrogate printers than using VBScript.
Topics for PowerShell’s Printers
- This Week’s Secret
- List All WMI Objects Containing the Word ‘Printer’
- List All Printers on Your Server
- List Printers Found in an Array of Computers
- Research More Properties for Win32_Printer with gm (get-Member)
♣
This Week’s Secret
I will show you how to use PowerShell to extract information about your printers. We will create a script which not only lists the printers, but also reveals their properties. For example, the printer’s sharename, driver, status and 90 other properties. The secret of this week’s scripts is the ease with which PowerShell exploits WMI (Windows Management Instrumentation) to provide information about your printers.
This Week’s Mission
This Week’s Mission is to get you started creating PowerShell scripts to reveal a printer’s properties and values:
- List all the printers on your server.
- Trawl the network and list the printers on named servers.
- Output the list of printers to a file.
- Investigate additional WMI classes for use with printers.
Preliminary Script 0: List All WMI Objects Containing the Word ‘Printer’
Many PowerShell cmdlets employ WMI to interrogate computer objects such as printers. As you may already know, get-WmiObject opens up a whole world of system objects; all we need to do is identify the Printer class of objects. The point of this initial research is to answer the question, ‘How did Guy know to use the class Win32_Printer?’
Instructions to Run PowerShell code:
If you have not used PowerShell before, here are step-by-stepinstructions to execute commands.
Method 1 (Quick)
- Launch PowerShell
- Copy the code into memory
(For instance, from Example below) - Right-click on the PowerShell symbol
- Edit –> Paste
- Press enter to execute the code
- See screenshot to the right
Method 2 (Best)
- Prepare to run cmdlets with this PowerShell command:
set-ExecutionPolicy RemoteSigned - Copy the code below into a text file.
- Save the file with a .ps1 extension, for example: network.ps1
- In PowerShell, navigate to where you saved network.ps1
- Issue this command:
.\network
(dot backslash filename)
# PowerShell script to list WMI Printer Objects
# Author: Guy Thomas
# Version 1.1 June 2008 tested on PowerShell v 1.0
get-WmiObject -list | where {$_.name -match "Printer"}
Note 1: In addition to Win32_printer (featured below) there is a WMI class for TCP/IP printing called: Win32_TCPIPPrinterPort.
Example 1: List All Printers on Your Server
# PowerShell for Printers
# Author: Guy Thomas
# Version 1.2 June 2008 tested on PowerShell v 1.0
$Class = "win32_printer"
get-WmiObject -class $Class | ft name, systemName, shareName
Learning Points
Note 1: Guy’s technique of using the variable $Class is optional.
Note 2: ft means: format-Table. What follows is a list of properties. Incidentally, you could append -auto.
Example 2: List Printers Found in an Array of Computers
The extra feature of Example 2 is the array. The idea is to create a list; in this case computers on your network, then loop the particular code to interrogate each computer in turn. As a preliminary step, amend the variable $arrayComp to hold the names of real servers on your network. Pay close attention to the speech marks and comma(s).
# PowerShell Array to List Printers
# Author: Guy Thomas
# Version 1.3 June 2008 tested on PowerShell v 1.0
$Class = "win32_printer"
$arrayComp ="BigServer", "LittleServer"
foreach ($machine in $arrayComp) {
get-WmiObject -class $Class -computername $machine |`
ft name, systemName, shareName -auto }
Learning Points
Note 1: To output this list of printers to a file, append this command:
out-File "c:\powershell\Printers\array.txt" -append
See Array strings in PowerShell »
Guy Recommends: WMI Monitor and It’s Free!
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Example 3: Research More Properties for Win32_Printer with gm (get-Member)
The objective of example 3 is to discover more WMI properties for win32_printer.
# PowerShell cmdlet to investigate win32_printer properties
# Author: Guy Thomas
# Version 1.2 June 2008 tested on PowerShell v 1.0
$Class = "Win32_printer"
$GMProp = get-WmiObject -class $Class | gm -memberType property `
| where {$_.name -notmatch "__*"}
$GMProp
$GMProp.count
Learning Points
Note 1: Use of -notMatch. "__" needs the star, hence "__*".
Note 2: By assigning the output to a variable, we can count the number of matching properties.
Note 3: The tiny backtick (`) is useful for explaining to PowerShell that the same command continues on the next line.
Note 4: Here is a more longwinded alternative to using the .count property:
| where {if ($_.name -notmatch "__*") {$i++}}
Guy Recommends: Tools4ever’s UMRA
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
Summary of PowerShell Printers
PowerShell can extract information about your printers. You can create a script which not only lists the printers, but also reveals their properties, for example, the printer’s sharename, driver, status and 90 other properties. I have used the printer examples to learn about WMI classes and also as a vehicle to create an array.