PowerShell for Windows Admins

May 6 2012   8:00AM GMT

Scripting Games 2012 comments: #13 Default Printer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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 WMIhttp://www.manning.com/powershellandwmi

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: