Reading Printer Properties (Part 2)

by Aug 5, 2021

In the previous tip we looked at Get-PrinterProperty which is part of the PrintManagement module and available on Windows operating systems.

In this tip, let’s check out how you can actually use the printer values in your own scripts, and how storing them in a hash table makes accessing printer properties much easier compared to working with simple arrays.

Important: the actual printer properties and their names depend on your printer model and printer driver. The property names used in the examples above may be different for your printer(s).

When you store the result returned by Get-PrinterProperty in a variable, the variable contains a simple array, and it is your job to identify the array element with the property you are after. Here is an example:

 
PS> $info = Get-PrinterProperty -PrinterName 'S/W Laser HP'

# return result is an array
PS> $info -is [Array]
True

# array elements can only be accessed via numeric index
PS> $info.Count
64

# the number of returned properties depends on the printer model
# the index of given properties may change so accessing a property by
# index works but can be unreliable:
PS> $info[0]

ComputerName         PrinterName          PropertyName         Type       Value
------------         -----------          ------------         ----       -----
                     S/W Laser HP         Config:AccessoryO... String     500Stapler


# the actual property value is stored in the property “Value”
PS> $info[0].Value
500Stapler

# using comparison operators, you can convert string content to Boolean
# for example to check whether a printer has a certain feature installed
PS> $info[2]

ComputerName         PrinterName          PropertyName         Type       Value
------------         -----------          ------------         ----       -----   
                     S/W Laser HP         Config:AutoConfig... String     NotInstalled

PS> $info[2].Value 
NotInstalled

PS> $info[2].Value -eq 'NotInstalled'
True 
 

A much safer way is to store the result in a hash table and use the original property names as keys. It is not well known that Group-Object can automatically create hash tables for you. Just tell Group-Object what the name of the property is that you want to use for grouping, and ask to get back a hash table and use strings as hash table keys:

 
$info = Get-PrinterProperty -PrinterName 'S/W Laser HP' | Group-Object -Property PropertyName -AsHashTable -AsString   
 

This time, $info contains a hash table, and if you use a PowerShell editor with IntelliSense like ISE or VSCode, once you add a dot to the variable name, you get rich IntelliSense showing available property names. In the console, you can press TAB to use autocompletion.

Since the IntelliSense menu and TAB completion also contains a few hash table-related properties and methods, you may have to scroll down a bit or press TAB a few times respectively.

To query the value of a printer property, once you picked a property name, you need to add quotes around the property name because typically, printer property names contain special characters like colons:

 
# hash table keys need to be quoted
PS> $info.Config:AccessoryOutputBins
At line:1 char:13
+ $info.Config:AccessoryOutputBins
+             ~~~~~~~~~~~~~~~~~~~~
Unexpected token ':AccessoryOutputBins' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken
  
  
# once you add quotes, all is fine
PS> $info.'Config:AccessoryOutputBins'
  
ComputerName         PrinterName          PropertyName         Type       Value          
------------         -----------          ------------         ----       -----
                     S/W Laser HP         Config:AccessoryO... String     500Stapler
  
PS> $info.'Config:AccessoryOutputBins'.Value
500Stapler  
 


Twitter This Tip! ReTweet this Tip!