PowerShell Tutorial – WMI Objects

Using WMI Objects in PowerShell

WMI, Windows Management Instrumentation is set of managed APIs provided by the Windows OS to expose information about local or remote computers.

You can use WMI objects from  PowerShell  by using the  Get-WMIObject cmdlet use to execute WMI objects as shown below

PowerShell WMI Objects

You can see the list of WMI objects available by executing Get-WMIObject -List as shown below:

PowerShell WMI Objects

More complex WMI queries such as the below example can be executed:

PowerShell WMI Objects

In the above query $QueryText is a variable which contains the WMI query which is similar to  a T-SQL style query.  This query extracts the logical disk information of C,D and G whereFreeSpace is greater than a certain value and the FileSystem is NTFS.

The below query gives the same result as above. Examine the way it has written, as it is a little different.

Get-WmiObject -Class
Win32_LogicalDisk | Where-Object {

 $_.DeviceID -eq "C:"
-or $_.DeviceID -eq "D:" -or $_.DeviceID -eq "G:"-and
$_.FreeSpace -gt 104857600 -and $_.FileSystem -eq "NTFS"} |
Format-Table

Personally I prefer the first version as it is more similar to T-SQL. Continues…