I have been looking at Windows PowerShell, and think it would be cool to use. We use WMI a lot at work, and I am wondering if I can gather the same kinds of information using Windows PowerShell that we use VBScript for? Is Windows PowerShell designed to do this?
– DB
Hi DB,
It is rather early in the morning down here in the southern part of the United States, and it is three hours earlier out in Redmond, Washington, in the USA. My tea pot is empty, so I am going to go make me a pot of tea and come back to you. Okay, back. Made a nice pot of Zhu Ye Qing tea. It has a lovely aroma and is a slightly sweet green tea. When paired with a small piece of 80 percent extra dark organic chocolate and maybe an ANZAC biscuit, it makes an awesome midafternoon tea. Because this is still early morning, I skipped the biscuit. Oh, yes, you want to know about using WMI with Windows PowerShell. No problem.
This week we will be looking at working with WMI from within Windows PowerShell. In honor of this week, we have created a new WMI Scripting Hub that pulls together a number of useful articles and tools from all over the Script Center into a single location. Because the articles will be using Windows PowerShell, you may also be interested in the Windows PowerShell Scripting Hub. You will find information there that will tell you how to download and install Windows PowerShell. |
I know this is the Hey Scripting Guys! article, but when working with Windows PowerShell and WMI it is often not a requirement to write a script. We can actually do some pretty good things without needing to write a script. I use the following command from the Windows PowerShell console to see how much free disk space I have on my computer.
Get-WmiObject -Class Win32_LogicalDisk
That is it. It will display the most commonly requested information about logical disks. This is seen here:
While the command seen above is short and easily typed, it can be made easier by using aliases. The Get-WmiObject has an alias (or shortcut name) of GWMI—or gwmi, which is the way I usually type it. The class name is the default parameter for the Get-WmiObject cmdlet, so we do not need to use the –class either. Using this super short syntax, the command becomes this:
gwmi win32_LogicalDisk
That command is shorter than a short-haired miniature Dachshund puppy running on a sandy beach in June.
To perform the same command in VBScript, and to retrieve exactly the same information in VBScript, you would need to use a script that looks similar to ListLogicalDiskDetails.vbs, which is seen here:
On Error Resume NextConst wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20
StrComputer = “.” WScript.Echo Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\CIMV2”) Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_LogicalDisk”, “WQL”, _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems WScript.Echo “DeviceID: ” & objItem.DeviceID WScript.Echo “DriveType: ” & objItem.DriveType WScript.Echo “FreeSpace: ” & objItem.FreeSpace WScript.Echo “ProviderName: ” & objItem.ProviderName WScript.Echo “Size: ” & objItem.Size WScript.Echo “VolumeName: ” & objItem.VolumeName WScript.Echo Next
Suppose I want information about the parallel port on my computer. I can use this command (again reverting to the long syntax for clarity):
Get-WmiObject -Class Win32_ParallelPort
That is it. You can also see the same command written in VBScript. The output from running our Windows PowerShell command is seen here:
To obtain information about my sound card by using VBScript, it takes nearly 20 lines of code, but it can be written as this single line in Windows PowerShell, if I want to use the alias for Get-WmiObject and rely upon the –class parameter being the default parameter:
gwmi win32_sounddevice
That one-line command retrieves all the information that WMI supplies about sound cards, the same as the previously mentioned VBScript. This is seen here:
If you are interested in obtaining information about your desktop monitor, you can use the Win32_DesktopMonitor WMI class on Microsoft Windows operating systems before Vista. Beginning with Windows Vista, the Win32_DesktopMonitor WMI class does not return reliable information because it is not compatible with the Windows Display Driver Model (WDDM). On Windows Vista and later, you will need to use the Win32_VideoController class. If you are afraid you will get confused, you can use Win32_VideoController on Windows XP and Windows Server 2003 as well as Windows Vista and later. To retrieve this information using Windows PowerShell, you would use the command seen here, or you can use the GWMI alias (to write the same script in VBScript would take more than 50 lines of code):
Get-WmiObject -Class Win32_VideoController
The results of this command are seen here:
I realize I am being unfair to VBScript, and this is not intended to be a head-to-head comparison. In fact, I love VBScript. I have written three books on VBScript, and spent more than 5 years traveling around the world, visiting nearly 20 countries in the process, teaching VBScript via the different workshops I wrote. Because Windows PowerShell was written after VBScript, things that were confusing and cumbersome to do in VBScript are ridiculously easy to do in Windows PowerShell. That simply is not fair! The good thing is we can turn this devious trick on its head, and leverage it for our own purposes. WMI is super easy to use from the Windows PowerShell prompt. Sneak attack or not, I will accept it.
This article talks about using the win32_processor class on Windows Vista. Note the examples, which are using VBScript, and compare them to this syntax:
gwmi win32_processor
Well, DB, I will let you go. Get Windows PowerShell, install it on your workstation (or on the computer belonging to your kids or significant other if you are afraid of messing up your own computer), and practice the techniques in this article. Go to the Script Center Script Repository, and look through the scripts there, particularly in the hardware section. See you tomorrow when we will continue with WMI Week. Until then, peace!
Ed Wilson and Craig Liebendorfer, Scripting Guys
0 comments