In this exercise, we will explore the use of WMI to obtain information about printers defined on your machine. As we do this, we will examine the use of several Windows PowerShell cmdlets that can dramatically simplify the use of WMI in our scripting.
Start Windows PowerShell by using Start | Run | PowerShell.
Open Notepad.exe or your favorite script editor.
Open the WmiTemplate.ps1 file from the scripts folder for this chapter and save it as yourname WorkingWithPrinters.ps1. Refer to the WorkingWithPrinters.ps1 script already in this chapter’s script folder to see a sample of a completed solution to this exercise.
Modify the $wmiQuery line to query the WIN32_Printer class. The revised line is shown here:
$wmiQuery = "Select * from win32_Printer"
Save and run the script. Your output will be similar to the following partial output:
Status : Unknown Name : Lexmark 4039 Plus PS __GENUS : 2 __CLASS : Win32_Printer __SUPERCLASS : CIM_Printer __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_Printer.Device __PROPERTY_COUNT : 86 __DERIVATION : {CIM_Printer, CIM_LogicalDevice, CIM_LogicalEleme nt, CIM_ManagedSystemElement} __SERVER : MRED1 __NAMESPACE : root\cimv2 __PATH : \\MRED1\root\cimv2:Win32_Printer.Devicelistitem">Change the script so that it only prints out the name of each printer. To do this, add the name property to the Format-List cmdlet, as shown here:
$objWMIServices | Format-List name
Save and run your script. You will see an output similar to the following:
name : Microsoft XPS Document Writer name : Lexmark 4039 Plus PS
To determine where each printer is defined, add the portname property to the Format-List cmdlet. This is shown here:
$objWMIServices | Format-List name, portname
Save and run the script. The resulting output will be similar to the one shown here:
name : Microsoft XPS Document Writer portname : XPSPort: name : Lexmark 4039 Plus PS portname : IP_192.168.1.89
To identify the capabilities of each printer, add the capabilitydescriptions property to the output. To do this, revise the Format-List line, as shown here:
$objWMIServices | Format-List name, portname, capabilitydescriptions
The resulting output is shown here:
name : Microsoft XPS Document Writer portname : XPSPort: capabilitydescriptions : {Copies, Color, Duplex, Collate} name : Lexmark 4039 Plus PS portname : IP_192.168.1.89 capabilitydescriptions : {Copies, Color, Duplex, Collate}
To determine whether there are other properties you may be interested in using from the WIN32_Printer WMI class, you can pipeline the object contained in the $objWmiServices variable to the Get-Member cmdlet. To reduce typing, use the gm alias, as shown here:
$objWMIServices | GM
If you find additional interesting properties, add them to the Format-List line as we did with the name, portname, and capabilitydescriptions properties. Some you may wish to investigate include driverName, ServerName, HorizontalResolution, and Vertical-Resolution.
This concludes this one step further exercise.