Microsoft Scripting Guy Ed Wilson here. Yesterday, the Scripting Wife installed Windows Power Shell 2.0 on her Windows XP Service Pack 3 computer. She used Microsoft Update to add the .NET Framework before completing the installation of Windows PowerShell 2.0. After updating the system, installation of Windows PowerShell took less than a minute.
The Scripting Wife spent some time reviewing two Hey, Scripting Guy! Blog posts (actually, she printed them out for reference). The first one is Hey, Scripting Guy! Windows PowerShell: An Introduction and the second one is Hey, Scripting Guy! Windows PowerShell and Pipelining. After reading those two articles, she came in to the library where I was reclining and writing a script, as shown in the following photo.
“I don’t know how in the world you expect people to be able to use Windows PowerShell,” the Scripting Wife stated flatly.
“Pardon?” said I.
“I have read the two most basic getting started articles you referred me to, and I don’t get it.”
“What am I supposed to do? How does using Windows PowerShell help me? There is not even a Help file. This is stupid! I don’t know why I even agreed to enter the 2010 Scripting Games. I am going to look blonde. If I embarrass myself, you are going to have to learn to cook real fast,” she calmly replied.
“Dudette,” I said, “slide on over here and I will help you get started.”
Once she plopped down on the other side of the love seat, and I adjusted my laptop computer screen, I continued.
“The first thing you need to know is that Windows PowerShell uses things called cmdlets (pronounced “command lets”). Cmdlets are sort of like programs, but they are only available inside the Windows PowerShell console or from within a Windows PowerShell script. You can write Windows PowerShell scripts using the Windows PowerShell Integrated Scripting Environment (also called the ISE). We will talk about writing scripts later. For now, we want to focus on understanding how these Windows PowerShell cmdlets work,” I paused to catch my breath and to see if she was still listening. Cool, she is still with me, and no longer breathing veiled threats against my personal culinary future.
“Windows PowerShell cmdlets all have two-part names. The first part is called a verb, and in many cases, it actually is a verb such as Get, Set, Write, and Add. A few verbs are not actually just verbs such as ConvertFrom or ConvertTo, but for the most part the verbs are actually just the verbs themselves. The second part of the Windows PowerShell cmdlet name is called the noun, and it designates the target of the operation. For example, to get a process, you use the Get-Process cmdlet. To get information about a service, you use the Get-Service cmdlet. What do you think the cmdlet name would be if you wish to get information from an event log?” I asked.
“Get-EventLog,” she guessed.
“That is absolutely correct. See, you get it. If I want to get a random number, what do you think the cmdlet would be?”
“Get-RandomNumber,” she answered.
“Close. Real close. It is Get-Random, but you are correct, the cmdlet could have easily been Get-RandomNumber. You are on the right track. Now, look over at the screen (see the following image) and you can see how easy it is to use these cmdlets. First, let’s type Get-R and press TAB. Pressing TAB will activate the tab expansion feature, and it will complete the command if there is only one match. Because there is only one command that has Get-R in its name, the command goes ahead and completes the name for you. As you can see, it creates a random number. Now let’s use the Get-Process cmdlet to return information about processes. Type Get-P and press Tab. What happens?” I asked.
“It did not work. It says Get-PfxCertificate,” she replied in a somewhat confused voice.
“That’s what happens when there are more than one cmdlets that match what you have typed. You can press TAB again and again until it displays the Get-Process cmdlet, or you can add the “r” to Get-Pr and press TAB. After the command is completed, press ENTER,” I said.
“By default when you start Windows PowerShell, it sets the working directory to your home directory,” I said.
“Why does it do that,” asked the Scripting Wife.
“It does this to ensure you have rights to save files and to read files from the directory. Yesterday, when you installed Windows PowerShell 2.0, I logged you on as the Administrator account so you would not have any problems with the installation, but today you logged on using your normal user account, NWTRADERSTeresa. As a normal user, you do not have access to everything. However, you always have rights to your personal folders.”
“Oh, I see. But I don’t really like this. Half of the command line is missing. Can you fix that?” she asked.
“You can use the same command that is used in DOS,” I started.
“That’s a big help. The Scripting Wife don’t do DOS,” she explained.
“Okay, just type CD. It stands for change directory. Point it to the root of the “C” drive, ” I replied. She typed CD C: and pressed ENTER.
“Nothing happened,” she remarked.
“You need to put a backward slash after the colon. The backward slash is under the bar character, and typically found just above the ENTER key,” I hastily added.
“I know that,” she remarked.
The results are seen in the following image.
“Can I make Windows PowerShell open up at the root of the drive all the time, so I don’t have to type that CD C: command all the time?” she asked.
“Of course you can my dear,” I oozed. “The best way to do that is to create a shortcut to Windows PowerShell. To do this, go into Start/All Programs/Accessories/Windows PowerShell and drag the Windows PowerShell icon to the desktop while holding down the right mouse button. When you let go, it will ask you if you want to copy or move the icon here. Choose Copy. Now right-click on the freshly copied shortcut that is on your desktop, and click Properties. Look at the Windows PowerShell Properties dialogue box that appears. In the middle of the dialogue box on the Shortcut tab, there is a box that says Start in:. Do you see it?” I asked. (See the following image.)
“Yes,” she said.
“You need to change %HomeDrive%%HomePath% to C:” I said. (See the following image.)
“Okay. I did it. Now what?”
“Click Apply, and then click OK. The dialog box should close. After you have completed that, double-click your new shortcut and see if it worked.”
“That is much better. Now, how can I find all of the Windows PowerShell cmdlets that let me get stuff?” she asked.
“Use the Get-Command cmdlet. This time, type Get-Co and press TAB. Before you press ENTER, type –v. It will complete the parameter name for you and should now look like Get-Command –Verb. What you are looking for are cmdlets that use the verb Get, so go ahead and type Get after the –Verb parameter.”
“Okay. Can I press ENTER now?”
“Yes, “I replied.
“The results scrolled off the screen. I hate that. Can you fix that for me too?” she asked.
“Of course. First type cls and press enter. Cls stands for clear screen, and it will remove all the old commands from your Windows PowerShell console. Now press the up arrow a couple of times until your Get-Command –Verb Get command line appears. At the end of the Get-Command –Verb Get command, type a space, and the pipe or bar key (straight up and down line, like this: |). It’s normally on the same key that has the backward slash (). After you have typed the pipe character, type a space and the word more. The more command will cause Windows PowerShell to display one screen of information at a time. To go to the next page of information, press SPACEBAR one time,” I said. (This is seen in the next image.)
“That is cool,” she said, “thanks.”
“Okay, go play around with Windows PowerShell, and see how many of the Get cmdlets you can get to work. I need to Get-Scripting,” I said punnily.
On that note, the Scripting Wife shuffled off. It is Sunday after all, and one needs to be able to spend some time working on one’s own scripts. That is the joy of weekend scripting. Join us tomorrow for a new week on the Hey, Scripting Guy! Blog.
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