PowerShell -Query Parameter

PowerShell -Query Parameter

PowerShell’s -query parameter looks for a WMI Query Language (WQL) statement.  Invariably, the statement is introduced by a "Select * from …." construction.  In truth, "Select * from" was most useful in VBScript, but in PowerShell many a time the simpler statement works just as efficiently and with less confusion for beginners.

Preparation with PowerShell’s Get-Help

Before we use PowerShell’s -query parameter, let us investigate the master cmdlet Get-WmiObject.  In particular, we need to understand the position and syntax of the parameter.   Incidentally, you can use the alias gwmi instead of of Get-WmiObject.

# Help with PowerShell WMI object:
Get-Help Get-WmiObject

Note 1: If you prefer to see examples append -full, thus: help Get-WmiObject -full


-Query in Action

WMI queries use WMI Query Language (WQL), which is a subset of SQL.  The main types of queries are for events (as in eventlog) or data, for example disk or memory.  One possible point of confusion is that WQL uses different syntax for operators such as "=", whereas PowerShell uses "-eq". 

Query Disk Partition

# PowerShell query select:
Get-WmiObject -query "Select * from win32_DiskPartition" | Ft -auto

Note 2:   Remember the "Double Quotes" around your Select statement.

Note 3:  The above is a perfectly good example of "Select * from".  However, I have to admit that in this particular example you could simply omitting -query.  Thus the point of -Query select is when you want a sub-set of the data, and for that you need a short where statement.   See more on PowerShell’s Select * from.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

PowerShell Query, Select and Where

Get-WmiObject -Query "Select * from win32_DiskPartition `
where PrimaryPartition =’True’" | Ft -auto

Note 4:  The WQL version of where, differs from the PowerShell syntax for where-Object.  In particular, WQL uses old-fashioned symbols such as "=", whereas PowerShell would use "-eq".

Note 5:  PowerShell uses the ` (Backtick) to continue the same command on the next line.

Query Logical Disk for Free Space

# PowerShell cmdlet to display a disk’s free space
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command split over two lines by a backtick `
Get-WmiObject -query "Select * from win32_logicaldisk" `
|Format-Table $item -auto

Note 6:  Once again we can simplify the script by omitting the -query command.

Get-WmiObject win32_logicaldisk `
| Format-Table DeviceId, MediaType, Size, FreeSpace -auto

Example Where You REALLY Need -Query and Select *

Here is an example where we really needed he -query "Select * from …" construction.

# PowerShell -Query example

$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command split over four lines by backticks (`)
Get-WmiObject -computer YourMachine -query `
"Select $([string]::Join(‘,’,$Item)) from win32_logicaldisk `
where MediaType=12" | sort MediaType, DeviceID | `
Format-Table $item -auto

See more WMI tasks for PowerShell »

Summary of PowerShell -Query Parameter

PowerShell's -query select is useful for filtering data, particularly where you combine with a where statement.

If you like this page then please share it with your friends


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • Windows PowerShell   • PowerShell 3.0 Network

Win32_pingstatus   • WMI Win32_NetworkAdapter   • Win32_NetworkAdapterConfig

Disable NIC   • PowerShell -Filter  • PowerShell -Query   • PowerShell Select   • Free WMI Monitor

Please email me if you have any example scripts. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.