Weekend Scripter: Use PowerShell to Find the Version of Windows
Summary: The Scripting Wife learns about using Windows PowerShell to find computer hardware information in prep for the 2013 Scripting Games.
Microsoft Scripting Guy, Ed Wilson, is here. This morning did not start with a nice leisurely cup of tea on the lanai at home. Instead, it started by rushing around to get packed, loading up the vehicle, vying for a parking place at the airport, taking a shuttle from parking to the Charlotte terminal, standing in line for an hour for a security screen that was more intimate than my last physical exam, putting shoes and belt back on and repacking my suitcase, and standing in another really long line to buy an overpriced cup of warm milk with a shot of acidic coffee in it. Then another long line to board a really, really cramped airplane. Luckily, the Scripting Wife is with me, and she helps make the entire process a little more pleasant. Yep, we are finally on our way to Seattle for the Windows PowerShell Summit.
Scripting Wife uses WMI
After takeoff, I open my laptop and the Windows PowerShell console and start playing around. Before long the Scripting Wife is poking me in my shoulder.
“Yes,” I say.
“So I think this would be the perfect time for you to talk to me about using WMI with Windows PowerShell,” she stated.
“OK, good idea,” I said as I slid my laptop over to allow her to use it.
“So what now?” she asked.
“Well the first thing to know about WMI is that there are lots of WMI classes. They are also arranged into different namespaces, but we will not get into that right now. To find WMI classes, use the Get-CimClass cmdlet in Windows PowerShell 3.0,” I said.
“Well, that makes sense. So what do I do if I need to find the name of my operating system?” she asked.
“Why don’t you try it? Use the Get-CimClass cmdlet, and put os into a pair of wildcard characters,” I suggested.
She typed the following:
The command is shown here:
The command and the associated output are shown in the following image.
“Well, that did not do what I expected it to do,” she said.
“So, you have found out that WMI does not really have a class named OS, or anything similar to OS. What are you really looking for?” I asked.
“I want to know the version of Windows,” she said.
“The cool thing about Get-CimClass is that you can search for classes that contain a specific property. So why don’t you look for WMI classes that contain a property of version?” I suggested.
The Scripting Wife typed the following:
The following is the command she created.
Get-CimClass -PropertyName version
The command and the output associated with the command are shown in the image that follows.
“It is still a lot of stuff,” she complained.
“Well one of the things you need to know about WMI is that there are abstract and dynamic classes,” I began.
“And why do I need to know that?” she interrupted.
“Maybe you do not need to know all that, but keep in mind you want to use dynamic classes,” I began again.
“Why?” she asked.
“Because dynamic WMI classes go off and get information. So they are the ones that you want to use. The abstract classes are more used by developers,” I said.
“Well OK. So I only need to remember to use dynamic classes. I got it. Now what?” she said.
“The Get-CimClass cmdlet has a QualifierName parameter. You can specify that you only want dynamic WMI classes returned. It will give you a better output,” I said.
“Cool. So let’s do it,” she said energetically.
“Use the Up arrow to retrieve your previous command. Go to the end of the command and add the QualifierName parameter,” I said. “And don’t forget to use tab completion to help you.”
The Scripting Wife typed:
Here is the command she created:
Get-CimClass -PropertyName version -QualifierName dynamic
The command and associated output are shown here.
“Cool,” she said. “So where do I find the version?”
“Look at the output. See the Win32_OperatingSystem?” I started.
“Oh yeah, I get it,” she said.
“Now use Get-CimInstance to retrieve the operating system information,” I said.
She typed the following:
Here is the command:
Here is the command and the output from the command:
“Well ain’t that just special,” she said as she pushed the laptop back over to me. She then opened her Surface and began reading a book that she had downloaded.
I returned to my Windows PowerShell console, and tried to make sense of life.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy