PowerShell Printer Scripts

PowerShell PrintersPowerShell Printer Scripts - win32_printer

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.

Topics for PowerShell’s Printers

 ♣

Tasks for PowerShell Printer Scripts

Despite being one of the first devices to be ‘plug and play’, printers still give more than their fair share of problems.  One clue of trouble ahead is the large number of adjectives to qualify the context of the word printer, for example, driver, device, default, network and share.  All this means is that printers provide rich pickings for writing good PowerShell cmdlets.  Here are possible tasks for a printer script:

  • 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.
  • Use PowerShell to add printer share.

Preliminary Script:  List All WMI Objects Containing the Word ‘Printer’

Many of these printer cmdlets employ WMI to interrogate computer objects such as printers.  As you may know, Get-WmiObject opens up a whole world of system objects, which you can then use PowerShell to read their values.

The point of this initial research is to answer the question, ‘How did Guy know to use the class Win32_Printer?’

Instructions

If you have not used PowerShell before, here are step-by-stepinstructions to execute commands. Else, just rely on the trusty copy and paste method.

# PowerShell to list WMI Printer Objects
# Author: Guy Thomas
Get-WmiObject -List | where {$_.name -Match "Printer"}

Note 1: In addition to Win32_printer (featured below) there is a class for TCP/IP printing: Win32_TCPIPPrinterPort.

Note 2: $_ means in this pipeline.

Example 1: List All Printers on Your Server

# PowerShell Script for Printers
# Author: Guy Thomas
Clear-Host
$Class = "win32_printer"
Get-WmiObject -class $Class | ft name, systemName, shareName -auto

Learning Points

Note 3: Guy’s technique of using the variable $Class is optional.

Note 4: FT means: Format-Table with the following properties.  Incidentally, you could append -auto.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

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 PowerShell 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 2:  List Printers Found in an Array of Computers

Preliminary, amend $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
$Class = "win32_printer"
$arrayComp ="BigServer", "LittleServer"
foreach ($machine in $arrayComp) {
Get-WmiObject -class $Class -computername $machine |`
ft name, systemName, shareName -auto } 

Learning Points

Note 5: To output this list of printers to a file, append this command:
out-File "d:\powershell\Printers\array.txt" -append

Example 3:  Research More Properties for Win32_Printer with gm (Get-Member)

# PowerShell cmdlet to investigate win32_printer properties
# Author: Guy Thomas
$Class = "Win32_printer"
$GMProp = Get-WmiObject -class $Class | gm -memberType property `
| where {$_.name -Notmatch "__*"}
$GMProp
$GMProp.count

Learning Points

Note 6: Use of -NotMatch.  "__" needs the star, hence "__*".

Note 7: By assigning the output to a variable, we can count the number of matching properties.

Note 8: The tiny backtick (`) is useful for explaining to PowerShell that the same command continues on the next line.

Note 9: Here is a more longwinded alternative to using the .count property:
| where {if ($_.name -Notmatch "__*") {$i++}}

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 4: PowerShell Adds a Network Printer

Preliminary step: to get this script to work you must amend ‘Server\PrintShare’ to reflect the real name of a server and a printer on your network.

# PowerShell add share printer
# Author: Guy Thomas
$PrinterPath = "\\Server\PrintShare"
$net = new-Object -Com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)

Note 10: You must edit the value for $PrinterPath to reflect a computer on your network.

Note 11: This PowerShell script does not use WmiObject with its Win32 class, but instead it uses:
New-Object -Com WScript.Network.

Note 12: Troubleshooting There should be no space before the bracket, this mistake caused me some head-scratching, until I realized that for once there was no spelling mistake in AddWindowsPrinterConnection, just an unwanted space:
$net.AddWindowsPrinterConnection  ($PrinterPath)

See more on PowerShell Logon Scripts »

Summary of PowerShell Printer Scripts

Ever since I can remember, printers give more problems than any other hardware.  PowerShell is not the magic bullet to cure all your printer problems, however, PowerShell makes it easier to configure printers than using the same commands in VBScript.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.