Windows PowerShell


PowerShell for Printers

PowerShell for 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

 ♣

Let us consider possible uses 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.
  • Map printers.

Preliminary Script  0:  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-step instructions to execute commands. Else, just rely on the trusty copy and paste method.

# PowerShell to list WMI Printer Objects
# Author: Guy Thomas
# Version 1.2 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 class for TCP/IP printing: Win32_TCPIPPrinterPort.

Example 1: List All Printers on Your Server

# PowerShell for Printers
# Author: Guy Thomas
# Version 1.3 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 with the following properties.  Incidentally, you could append -auto.

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
# 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 "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
# 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++}}

Example 4: To Add or 'Map' 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 for Mapping a Printer
# Author: Guy Thomas
# Version 1.2 June 2008 tested on PowerShell v 1.0

$PrinterPath = "\\Server\PrintShare"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)

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

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

Note 3:  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)

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Summary of PowerShell and Printers

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.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.