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.
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:
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:
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.
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.
PowerShell Query, Select and Where
Get-WmiObject -Query "Select * from win32_DiskPartition `
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
Note 6: Once again we can simplify the script by omitting the -query command.
Get-WmiObject win32_logicaldisk `
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")
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:
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.