Learn about Windows PowerShell
Summary: The Scripting Wife needs to get ready for the 2012 Scripting Games, and she uses Windows PowerShell to get hardware info from her laptop.
Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I were in Myrtle Beach, where I was working on the Scripting Games over the weekend. The trip to the Carolina Beaches is rapidly becoming an annual migration of the rare but not endangered Script Monkey. Anyway, I was on the lanai reading my email this morning, when I heard her—or more accurately—I felt her enter the room.
“So where are you taking me for Valentine’s Day?” she queried.
“I am taking you to the Charlotte IT Pro User Group meeting,” I said.
“You’re kidding. Right?”
“Nope, that is why I took you to Ruth’s Chris Steak House on Friday night. You knew that.”
“You took me to Ruth’s Chris because you had a gift card,” she countered.
“You knew it was your Valentine’s Day dinner. We had a lovely time. Now what’s the problem?” I asked.
“No problem. I was just trying to see if you were awake. Good morning, my little Script Monkey.”
“By the way, what are you doing up so early? It is not yet noon,” I queried.
“Ha ha. So funny,” she intoned. “Just so you will know, I have been up for hours. I am working on the 2012 Scripting Games Study Guide.”
“Uh huh. I see. Well, that is great. Good luck. Let me know how it is working for you,” I replied pleasantly enough.
“Well, my little Script Monkey, I do have a question.” She was dripping with sincerity.
“Yes,” I replied with more than a modicum of hesitancy. (I was never going to get through the 2,000 emails in my inbox at this rate.)
“I am a little rusty with Windows PowerShell. In fact, I have pretty much not touched it since last year,” she confessed.
“Huh? But all that time you spend at your computer. I thought you were at least writing a script.”
“Facebook. Twitter. PowerScripting PodCast. My blog. I get busy. Besides, if I really need a script, I figure you will write it.”
“You might just figure wrong too,” I said smiling. “So what is your problem?”
“Well, I forget how to find stuff out about hardware. I figure that will be the first event, because your Study Guide lists it first.”
I smiled, but did not commit myself. Paused for a second. Then replied, “OK, sit down.” She took a seat on the lanai swing.
“Now, to find out stuff about the hardware in Windows PowerShell 2.0, it is normally going to require using WMI,” I continued.
“I would use the Get-WMIObject cmdlet to find that out?” she asked.
“Absolutely. Now, write a command that will get information from the BIOS,” I said.
“Well, how do I know that? That is why I am here, silly,” she chastised.
“OK. One of the nice things about the Get-WmiObject cmdlet is that it can tell you what class you need. To do this, use the List parameter, and then use wild cards around the word BIOS.”
I slid the laptop over to her. She thought for a minute, and hesitantly typed the following:
Note (In the commands that follow, <tab> represents one press of the Tab key. One press of the Space bar is <space>. If you see two tab characters, such as <tab><tab>, it means press the Tab key twice. For most computers, this will probably work, but depending on what modules and other items are loaded in your environment, you may need to press the Tab key more than the indicated number of times. Always compare your completed command with the commands that are indicated as complete.
When the Scripting Wife finished typing, she turned the laptop back to me and smiled. “Is this the right command?” she asked.
I looked at it. The command she had composed is shown here.
Get-WmiObject -List *bios*
“That is exactly right. Now, which WMI class do you think actually reports only information about the BIOS on your computer?” I asked.
She looked at the output, which is shown in the image that follows.
“I think the Win32_BIOS is the right WMI class…because it sounds right, and the properties sound like BIOS things,” she postulated.
“And it begins with a Win32 which sounds like Windows. You told me to always use WMI classes that start with Win32,” she said.
“Very good. Now, go ahead and query that class,” I said.
She started to ask how, then bent over the laptop and began to type. She did the following:
The command she created is shown here.
Following are the command and its associated output.
“It seems that my screen is getting cluttered up,” she complained.
“Type the command cls…that is a shortcut for the Clear-Host function,” I instructed.
The scripting wife typed the following:
“Now, just for fun, use the Get-History cmdlet to see what commands you have typed,” I instructed.
She typed h (a shortcut for the Get-History cmdlet name). Here is her command.
The output is shown in the image that follows.
“Press the Up arrow four times, erase the word bios*, and replace it with *processor*,” I suggested.
The Scripting Wife typed the following commands.
<up arrow><up arrow><up arrow><up arrow><backspace><backspace><backspace><backspace><backspace>*processor*<enter>
Get-WmiObject -List *processor*
The command and its associated output are shown in the image that follows.
“Now, can you pick out the WMI class that will tell you about the processor on your computer?”
“The Win32_Processor class will do that because it begins with Win32, and it has properties like AddressWidth, Architecture, and Availability,” she said.
“Absolutely right. Now go ahead and clear your screen, and then query the class.”
She did not even hesitate. Not for a second. She immediately did the following.
The command she that created is shown here.
The output from the command is shown here.
The Scripting Wife handed the laptop back to me. She smiled and jumped off the swing.
“Well, I guess I will see ya,” she said cheerily.
“Huh,” I asked.
“Yep. If we have to go to the Charlotte IT Pro User Group for Valentine’s Day, then it is going to take me all day to get ready,” she explained.
“I see. And why is that?”
“Well, I have to get my hair done. Then I have to go to the spa and get a manicure and a pedicure. I think I will invite my friends out for lunch, and then we will go shopping so I can get a new outfit to wear to the meeting. Really. Don’t you know anything?”
With that, she bounded out the front door of the lanai, and was gone. Really, sometimes I wonder if I do know anything about the Scripting Wife.
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
It was fun to read Scripting Wife's articles last year, but this year it's even better because I have no issues to almost replay whole scenes in my head. ;) Glad I had to meet you both last year. :> Waiting for more! :D
Hi Teresa, Hi Ed,
I'm very glad to hear that Ed is diving deeper into 2012sg :-)
( Hope you're stilling enjoying life *sss* :-)
Well done Teresa!
Exploring the WMI classes and getting back Hardware information is really one the highlights that Powershell offers ... especially running queries on remote computers!
It's quite a snap in PS to unleash the power of WMI and I hope that future versions of PS will even extent WMI support to optimize information retrieval.
Efficiently working with the console using tab completion, the history and even the mouse is obviously something everybody should know!
Just remember to enable the Quick-Edit Mode of the console window (right click the title bar, choose properties and turn on QuickEdit mode - for this and all equally named windows)
Excellent scenario and very informative!