PowerShell WMI

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



Types of WMI Class in PowerShell

Types of WMI Class in PowerShell

Windows Management Instrumentation (WMI) provides a way of accessing details of your operating system that are normally hidden from the Control Panel, Device manager or Windows Explorer.  Every time I review WMI it amazes me the number and variety of these probes, or WMI Classes that are available to PowerShell's get-WmiObect.

Topics for WMI Classes in PowerShell

 ♣

PowerShell Tutorial to List WMI Classes

Pre-requisites:  Visit Microsoft's site and download the correct version of PowerShell for your operating system.

  • Launch PowerShell (Preferably ISE)
  • Copy the lines of code below (into memory)
  • Right-click on the PowerShell symbol
  • Edit --> Paste
  • Press enter to execute the code.

PowerShell Command to Enumerate All WMI Classes

# PowerShell classes of WMI
# Author: Guy Thomas
# Version 1.4 February 2010 tested on PowerShell v 1.0 and 2.0

get-WmiObject -list

Note:  As usual, you can view all a PowerShell cmdlets parameters with get-Help, for example:
get-Help get-WmiObject.

To List Every WMI Class Beginning with Win32

Although not all PowerShell cmdlets use the -list parameter, get-WmiObject does support this handy enumerator.  However, there is a problem with the base command: get-WmiObject -list, namely the overwhelming number of classes returned.  For this reason I have introduced extra code which filters classes relevant to a particular project; for example, network classes. One of the most useful subsets of WMI classes is the one beginning with Win32.  This example will give you a count of the Win32 classes.

# PowerShell example to list all Win32 WMI classes
# Author: Guy Thomas
# Version 1.4 February 2010 tested on PowerShell v 1.0 and 2.0

clear-Host
$i=0
$Type = "Win32"
$WMI = get-WmiObject -list | Where-Object {$_.name -match $Type}
Foreach ($Class in $WMI) {$Class.name; $i++}
Write-Host 'These are the ' $i' types of '$Type

Learning Points

Note 1:  This script employs 3 variables, $i to count the instances, $Type to hold the string you wish to filter, and $WMI to initiate the loop which counts the object classes.

Note 2:  One side-effect of introducing extra PowerShell techniques is that I have made the script unnecessarily long and complex.  For simplicity, you could strip the script down to this one command:

get-WmiObject -list | Where-Object {$_.name -match "Win32"}

Challenge:  Amend my example script to display extra information

Challenge:  Observe and trace how PowerShell's -match parameter compares the value held by $Type.  Incidentally, you could experiment with "CIM" instead of "Win32".

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 To List Only WMI Network Classes

The additional feature of this script is that refines the search from the broad 'Win32', to the narrower 'Win32_Network'.  The result is a list of network WMI classes.

# PowerShell example to list every WMI class matching Win32_network
# Author: Guy Thomas
# Version 1.5 February 2010 tested on PowerShell v 1.0 and 2.0

clear-Host
$i=0
$Type = "Win32_network"
$WMI = get-WmiObject -list | Where-Object {$_.name -match $Type}
Foreach ($Class in $WMI) {$Class.name; $i++}
Write-Host 'There are' $i' types of '$Type

Learning Points

Note 1:  In practical terms, most of the 7 network classes are disappointing.  However, this is a feature of WMI, it sounds exciting to find so many classes, but when it comes down to practicalities, only one or two are truly useful for any given task.

Note 3: You could use the alias PowerShell gwmi instead of get-WmiObject

How to Research WMI Properties with get-Member

Nearly everyone who scripts with PowerShell calls for help with get-Member.  Some experts freely admit employing this technique, while others pretend that they can memorize all the properties.  (One or two sad cases really can, and consequently, will quote all the Properties of any class you care to name).

I cannot emphasise too strongly how important get-Member is in mastering PowerShell.  Not only can you apply get-Member to every other PowerShell cmdlet, but also third-party add-ons such as QAD support this help system.

clear-Host
get-WmiObject -class Win32_NetworkAdapterConfiguration | get-Member

Note 1:  The secret to success with get-Member is to remember the (|) pipeline symbol before you append this get-Member cmdlet.

Note 2:  If you are overwhelmed with information you can add the -MemberType parameter with a value of 'property' hence: get-Member -MemberType property [a-z]*.

Note 3:  Before we leave get-Member a reminder that even this command accepts get-Help, thus you could try: get-Help get-Member -full.  My point is that help will reveal other options, for example -MemberType Method.

Summary of Types of WMI Class

The key information that PowerShell's get-WmiObject needs is a WMI class.  The examples on this will help you to research the best class for your task.  Those WMI Classes beginning with Win32 are a particularly rich source, however to refine your search try adding a PowerShell -match statement.

See More Microsoft PowerShell WMI Examples:

Home   • PowerShell WMI   • WMI Services   • Memory   • Disk   • DNS   • Win32_pingstatus

WMI Class   • Win32_NetworkAdapter   • Win32_NetworkAdapterConfiguration   • Disable NIC

Examples   • Win32_product   • PowerShell -Query   • PowerShell -Filter

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.