Learn about Windows PowerShell
This week we are following the Scripting Wife as she pushes ahead with her preparations for the 2010 Scripting Games. You can catch up with her progress by reviewing the articles in the archive.
Microsoft Scripting Guy Ed Wilson here. I lean back in my chair, close my eyes, and recite: “So many hours must I tend my flock, so many hours must I take my rest, so many hours must I contemplate….” I am starting to get the feel for the rhythm when suddenly the overhead light is switched on, and a loud voice cries out.
“So there you are,” the Scripting Wife proclaims triumphantly. “Thought you could pull a fast one and hide in the guest bedroom did you? What are you doing anyway?”
“I am going over my lines. My Shakespeare group has to perform Henry VI, Part 3,” I said.
“Oh yeah, is that tomorrow? I can’t wait to see it. I got front row tickets, and I am going to make faces at you all through the performance,” she exclaimed mirthfully.
“You wouldn’t,” I said in mock terror.
“No. You are right. But it is fun to think about. By the way, if this is part three, what happened in the first two parts,” she asked, immediately regretting her decision.
“Well, part one opens with Henry the Fifth’s funeral march…” I began.
“No. Strike that. I will look it up on the Internet. It will be way faster than listening to you. I have a question for you, which is why I was looking for you. I called your cell phone but you did not answer,” she added.
“I had it on vibrate and did not hear it,” I explained.
“I was looking at the 2010 Scripting Games Study Guide, and it says I need to know something about WMI. I clicked your links, but there was just too much information. I need the short version,” she pleaded.
“Okay. Short version. WMI stands for Windows Management Instrumentation, and while it has nothing to do with saxophones and flutes, it has everything to do with Windows and with management. For example, using WMI, I can find out information about how much free disk space I have, my computer name, the version of the operating system, or even what software is installed,” I said.
“Wow. So how would I find out about my operating system?” she asked.
“Well, you want to use a Windows PowerShell cmdlet called Get-WmiObject and a WMI class named Win32_OperatingSystem. To get the command, type Get-W and press TAB a couple of times until Get-WmiObject appears. Then type the class name Win32_OperatingSystem and press ENTER. Your command will look like Get-WmiObject Win32_OperatingSystem. Let me see it when you are done,” I instructed.
“Okay. This is pretty cool, but I do not understand it all,” she said, showing me her laptop.
“What do you not understand,” I asked.
“Well, it says my version is 5.1.2600, but I thought my version was Windows XP,” she replied.
“It is. Version 5 is the major version, 1 is the minor version, and 2600 is the build number. Put it all together, and you have Windows XP. Windows 7, for example is build 6.1.7600. That is also the build number for Windows 2008 R2, by the way,” I added.
“Okay. But I have a question. How did you know to use Win32_OperatingSystem?’” she asked.
“You know, that is an excellent question. It is actually one of the harder parts about using WMI. But with Windows PowerShell, you can find WMI classes fairly easily. For example, if you want to know about your monitor, you can search to see if there are any WMI classes. This time, we will use the alias for Get-WmiObject to make it easier to type. The alias is gwmi; so type gwmi –list and then the asterisk in front of and behind the word monitor. The asterisk in front of monitor is a wildcard character and means anything before the word monitor. By putting an asterisk behind the word monitor, it also will find anything behind; therefore, anything that has the word monitor in the middle of it will be returned by the command. So your complete command is gwmi –list *monitor*,” I said.
“Well, all it did was return a bunch of gibberish,” she complained.
“Gibberish, hmm, let me see,” I puzzled. “That’s not gibberish. It is the name of the WMI classes, and exactly what we were looking for.You want to use the classes that begin with Win32_. Can you tell me which class you will use to find information about your monitor?” I asked.
“From the list, it looks like Win32_DesktopMonitor,” she said.
“Exactly. To query it, use the Get-WmiObject cmdlet. Let’s use the alias again, gwmi Win32_DesktopMonitor,” I said.
“Wow, it returns a lot of stuff,” she said.
“If you would like to see only one property, you can put parentheses around it. Use the up arrow and retrieve your previous gwmi Win32_DesktopMonitor command. Put parentheses around it, use a period, and type the property name description. This will cause the command to return only the description of your monitor. The command you will type is (gwmi Win32_DesktopMonitor).Description,” I said.
“Yep. I have got it,” she said.
“Well, why don’t you go play around with WMI. See if you can find information about memory, disk drives, your processor, and the like? Keep in mind that WMI does not always call things the same thing you do. For example, there is no Win32_doohicky class, nor a Win32_thingamajig class. So you will need to experiment with several different names before you find what you are looking for. In addition, not all WMI classes return good information, but a lot of them do. There are more than 800 WMI classes on Windows XP, and more than a thousand on Windows 7; therefore, you are bound to run into something that is cool,” I advised.
“Okay, then, if that is all you want to show me today, I am outta here,” she exclaimed.
“Let’s see, where was I … ‘Ah, what a life were this, how sweet, how lovely!’” I said sounding just a bit like Olivier.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at email@example.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys