Learn about Windows PowerShell
Microsoft Scripting Guy Ed Wilson here. In some respects—nay, in many respects—the weekend should not be called weekend, but rather weekbeginning. With no meetings, no presentations, no e-mail, and no phone interruptions, the weekend offers endless possibilities, unmitigated avenues for exploration, and boundless opportunities to read, to relax, and to write scripts. I find that the way in which I use the weekend, the things I learn, the scripts I write, and the things I discover set the tone for the week to come. In this way, Saturday and Sunday are truly the weekbeginning.
I was sitting on the front porch thinking about a recent weekend excursion I took to the American Shakespeare Center in Staunton, Virginia, where they have the world’s only recreation of Blackfriars Theatre. The thrust stage of the playhouse breaks the fourth wall and provides a much more intimate setting. You can get a sense of the atmosphere in the following photo I took during the backstage tour.
With the lights left on, the actors can see the audience, and the audience can see each another, which provides for a very interactive setting. I got greedy and saw multiple plays in the same day. At first, I thought I had made a mistake, but the weekend flew by.
So what do Shakespeare and Windows PowerShell have to do with one another? In the Renaissance productions, they would not have had elaborate sets, and they used few props. With Windows PowerShell, you are given the bare basics. How you construct the scene and how you fit the pieces together are up to you.
As I reached for my teapot to refill my cup, the Scripting Wife made her entrance—not with a thunderclap, but with the sound of violins and songbirds singing.
“How now, my lord,” she said.
Okay, that is not actually what she said. But then, violins were not playing, and there were no songbirds singing either. What she really said was more along the lines of “what are you planning on fixing for dinner?” but I digress. After dispensing with the morning pleasantries, she got down to cases.
“I have a program that seems to be eating up all the CPU time on my laptop. It is making everything run slower than a centipede with athletes’ foot. I opened up Task Manager, and that is how I discovered the CPU munchfest, but I do not remember starting the program. I need to know when the program started,” she stated in a matter of fact manner.
“Fie on your program! Forsooth, we shall employ Windows PowerShell to delve into these mysteries,” saith I.
“You are not funny, you know,” she drolled.
“Okay, sit down over here, and let’s look at your laptop. Open Windows PowerShell.”
“Now what,” she asked.
“You know you can use the Get-Process cmdlet to retrieve a list of all the running programs. And then you can use Get-Process to retrieve information about a specific program by using the –name parameter for Get-Process,” I stated.
“Yes, but that does not tell me anything about the process start times,” she quipped while typing the commands. (The following image shows results of Get-Process.)
“That is true, by default. But if we pipe the command to the Get-Member cmdlet, you will see that there are additional pieces of information that Windows PowerShell can tell us. The Windows PowerShell team selects the things they think most people will wish to see the most to display by default. To get the additional properties of an object, we will need to pipe the results to another cmdlet,” I explained.
“Say what? I think I preferred your archaic Shakespeare imitation to this geek speak,” the Scripting Wife said in annoyance.
“Okay. Let’s start at the beginning. When you run a Windows PowerShell command, it does not return text. It returns an object. You see text in the Windows PowerShell console, but those are only the values of some of the properties of the object,” I said.
“Uh huh,” she said with an empty voice.
“Everything is an object. For example, your car is an object. It has properties such as color, number of doors, and number of tires. It also has methods such as the drive down the road method, or the stop at a red light method. Does this make sense,” I asked.
“So what you are saying is that properties describe things, and methods do things,” she summarized.
“Then why didn’t you say that in the first place,” she chastised.
“The pipeline is the bar character. On your laptop, it is the character above the backward slash key above the enter key,” I said. “To take the objects that are returned by one command, you send them through the pipeline character to the next command.”
“Can you show me,” she asked.
“You are looking for how long a program has been running. To see if there is a property that provides that information, you need to pipe the results of your Get-Process cmdlet to the Get-Member cmdlet. You will type it as Get-Process | Get-Member,” I said.
“Ok. Now what,” she said turning her laptop around so that I could see what is shown in the following image.
“Scroll down through the list of properties, and see if you see anything that might tell you how long a process has been running.”
“The StartTime property,” she said as she highlighted it with her mouse (see the next image).
“That’s pretty cool,” she admitted.
“If you want to display just the name, start time, and version of a particular program such as Notepad, you would type Get-P press TAB, type -n and press tab, type | Select- and press TAB, and then type the three property names: Name, StartTime, ProductVersion,” I instructed.
“Like this,” she said as she turned her laptop for my inspection. What I saw is shown in the following image.
“Absolutely,” I gushed.
“Okay. Thanks,” she said, as she departed stage right. (Yes, our house has a stage. Big Shakespeare fans.)
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 firstname.lastname@example.org or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys