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.
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 1: Remember the "Double Quotes" around
your Select statement.
Note 2: 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: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
Get-WmiObject -Query "Select * from win32_DiskPartition ` where
PrimaryPartition ='True'" | Ft -auto
Note 1: 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 2: 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 1: Once again we can simplify the script by
omitting the -query command.
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.