Microsoft Scripting Guy–Ed Wilson Guest Blog–PowerShell and WMI Discovery
Today we have a very special treat! A guest blog from the Microsoft Scripting Guy himself, Ed Wilson.
Ed Wilson is the Microsoft Scripting Guy and a well-known scripting expert. He writes the daily Hey Scripting Guy! blog,. He has also spoken at TechEd and at the Microsoft internal TechReady conferences. He is a Microsoft-certified trainer who has delivered a popular Windows PowerShell workshop to Microsoft Premier Customers worldwide. He has written 9 books including 6 on Windows scripting that were published by Microsoft Press. He has also contributed to nearly a dozen other books. His Windows PowerShell 2.0 Best Practices book for Microsoft Press was recently published. Ed holds more than 20 industry certifications, including Microsoft Certified Systems Engineer (MCSE) and Certified Information Systems Security Professional (CISSP). Prior to coming to work for Microsoft, he was a senior consultant for a Microsoft Gold Certified Partner where he specialized in Active Directory design and Exchange implementation. In his spare time, he enjoys woodworking, underwater photography, and scuba diving.
Enough with the introductions, sit back, relax and enjoy
I want to thank Nicolas Blank for providing me an opportunity to contribute to his blog.
One of the things I like about Windows PowerShell is that it makes it really easy to find things. Back in the days when I was writing VBScript code, I always had the VBScript documentation open, the WMI SDK open, and a page open to MSDN as well. These sources of documentation were pretty much a requirement to be able to write VBScript code from scratch.
Nowadays, I am not nearly as documentation heavy – not just because I have been writing scripts for so long, but because I can use Windows PowerShell to answer most questions I have about working with Windows PowerShell. However, not only Windows PowerShell, but also I can use Windows PowerShell to find out information about other things as well.
For example, If I am working with WMI and I need information about the bios on my computer, I can ask Windows PowerShell to see if there is a WMI class that will help me. Here is the command to find WMI classes related to the bios.
Get-WmiObject -List *bios*
The command and associated output appear in the following figure.
Now, once I have the list of possible matches, It is a simple matter of looking for a class that seems to provide the best match. I select the Win32_Bios class, and quickly query it by using the Get-WmiObject Windows PowerShell cmdlet as seen here.
Get-WmiObject Win32_bios
The command and associated output appear in the following figure.
But, suppose I did not know I wanted to query the Win32_ Bios WMI class, what would I do in that case?
Well, for one thing, I can use the Get-Member Windows PowerShell cmdlet to see the methods and properties that a class supports. This command is illustrated here.
Get-WmiObject win32_bios | Get-Member
If I want to see the members of all the WMI classes I found in the earlier, I can use the following command. (In the following command, gwmi is an alias for Get-WmiObject. The % symbol is an alias for the Foreach-Object cmdlet, and gm is an alias for Get-Member. )
gwmi -List *bios* | % { gwmi $_.name | gm }
The command and associated output appear in the following figure.
As you can see, using Windows PowerShell and WMI it is easy to find and to retrieve useful information that can be used to solve real world problems.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. I write about Windows PowerShell every day on the Hey Scripting Guy! blog. Until then, peace.
Ed Wilson, Microsoft Scripting Guy