Introduction to PowerShell’s [WMI] Type
The [WMI] Type technique is not for the beginner, for novices I suggest they concentrate on Get-WmiObject. Even for experienced PowerShell script writers I question the usefulness of the [WMI] Type construction. While it looks flashy, the syntax is picky, therefore I am not sure that it is better than the straightforward Get-WmiObject.
Topics for PowerShell [WMI] Type
- Get-WmiObject Refresher
- [WMI] Type – Simple Example
- [WMI] Type Get-Member
- [WMI] Type Picky Syntax
- Summary of WMI and PowerShell
♣
[WMI] Type – Simple Example
This example employs the called Win32_ComputerSystem. By introducing variables I highlight the WMI particular class, and also remind you to change the name of $Srv. Incidentally, neither LocalHost nor ‘.’ works for as a value for your computer name, see what I mean about [WMI] Type being picky?
# [WMI] Type example
Clear-Host
$Srv = "Longhorn"
$CimPlace = "Root\Cimv2:Win32_ComputerSystem"
[WMI]$LowDown = "$CimPlace.name=’$Srv’"
$LowDown
Note 1: The reason that the above example works is that luckily I have used the .name property, which is the correct primary key for the WMI class Win32_ComputerSystem.
Guy Recommends: Free WMI Monitor for PowerShell![Solarwinds Free WMI Monitor for PowerShell](data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMDUiIGhlaWdodD0iMTQyIiB2aWV3Qm94PSIwIDAgMjA1IDE0MiI+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgc3R5bGU9ImZpbGw6I2NmZDRkYjtmaWxsLW9wYWNpdHk6IDAuMTsiLz48L3N2Zz4=)
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
[WMI] Type Get-Member
As with other PowerShell cmdlets, once you have created the [WMI] Type object you can research its properties with Get-Member:
# PowerShell [WMI] Type Properties
Clear-Host
$Srv = "Longhorn"
[WMI]$LowDown = "Root\Cimv2:Win32_ComputerSystem.name=’$Srv’"
$LowDown | Get-Member -memberType Property [a-z]*
Note 2: With this example I have defined [WMI]$LowDown more directly.
Alternative Method [WMI] Root..
# PowerShell [WMI] Type Properties
Clear-Host
$Srv = "Longhorn"
$Stuff = [WMI]"Root\Cimv2:Win32_ComputerSystem.name=’$Srv’"
$Stuff | Get-Member -memberType Property [a-z]*
Note 3: This simply alters the position of the [WMI] Type from
[WMI]$LowDown = "Root\…
To
$Stuff = [WMI]"Root\..
Researching the Primary Key for a WMI Class
Here is an example which creates a function which extracts the primary key for any named or given WMI class.
# Function to Discover WMI Primary Keys
Clear-Host
Function Get-WmiKey
{
$Class = [WmiClass]$args[0]
$Class.Properties | `
Select @{Name="PName";Expression={$_.name}} -Expand Qualifiers | `
Where {$_.Name -eq "key"} | `
foreach {$_.Pname}
}
Get-WmiKey Win32_LogicalDisk
Note 4: The whole point of creating this function is that you can substitute any WMI class for Win32_LogicalDisk.
Note 5: Alternatively you can research WMI Classes with: WBEMTest.
Note 6: See more on PowerShell functions.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM)
v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
[WMI] Type Picky Syntax (Or Silly Me?)
Clear-Host
$Hardy = ""
[WMI]$Hardy="Root\CimV2:Win32_LogicalDisk.DeviceID=’C:’"
$Hardy
Note 6: This example drove me mad. The reason was that I introduced white space in an attempt to make the command more readable. Introducing a space either side of the equals sign produced the "Invalid parameter " error. Wrong, silly me.
.DeviceID = ‘C:’" Just does not work.
Neither did introducing a space between the two sets of speech marks:
.DeviceID=’C:’ " – Wrong.
See more WMI tasks for PowerShell »
Summary of PowerShell [WMI] Type
PowerShell’s [WMI] Type technique is not for the beginner. Even for experienced PowerShell script writers I question the usefulness of the [WMI] Type construction. While it looks flashy, the syntax is picky, and I am not sure that it is better than the straightforward Get-WmiObject.
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.