Scripting Games 2012 comments: #13 Default Printer

In one of the events you had to find the default printer. This can be done using WMI.

The full list of printers can seen using:

Get-WmiObject -Class Win32_Printer


If you want to examine the printer objecy – to determine what information is available – use get-member or select the first printer in the list

Get-WmiObject -Class Win32_Printer | select -f 1 | fl *


you will see that there is a property Default

PS> Get-WmiObject -Class Win32_Printer | Get-Member Default

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_Printer

Name    MemberType Definition
—-    ———- ———-
Default Property   System.Boolean Default {get;set;}


which is Boolean i.e. it has to return true or false


Your first thought might ne to do this:

Get-WmiObject -Class Win32_Printer | where {$_.Default -eq $true}


but it would be better coding practice to do this:

Get-WmiObject -Class Win32_Printer | where {$_.Default}


On the local machine this is OK but if you are working remotely than all of the Win32_Printer objects would be returned and the filtering performed locally. Could be an expensive operation.

The better option is to use the –Filter parameter on Get-WmiObject

Get-WmiObject -Class Win32_Printer -Filter “Default = $true”


this only returns a single  object

If you want to use WQL then it becomes

Get-WmiObject -Query “SELECT * FROM Win32_Printer WHERE Default = $true”


In either case the filtering is done early to reduce the amount of data you are dealing with.

Remember – Filter early & format late


For more information on working with printers see chapter 10 of PowerShell and WMI

This entry was posted in PowerShell and WMI, Scripting. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s