Learn about Windows PowerShell
Microsoft Scripting Guy Ed Wilson here. The Bradford pear trees are in bloom in Charlotte, North Carolina, in the United States. The small, delicate, white blossoms that grace the twisted shrub-like trees drape like pearls around the neck of a stately dowager. For a short two-week period, the Deep South is clothed in color, bringing out a hidden charm that seems to pervade the very social fabric of the town.
The cool temperatures, clear blue skies, and blooming trees conspire to draw me out of doors. The great thing about wireless networking is I can sit in a lawn chair on the deck in the back yard, and enjoy the beginnings of spring while also getting work done. To celebrate the change in seasons, I made a pot of green tea with jasmine.
I was working on a Windows PowerShell script for a future Hey, Scripting Guy! Blog post, when the Scripting Wife showed up carrying her laptop.
“Hey, Scripting Guy! I have a question,” she said as she pulled up a chair.
“Yes,” I replied.
“I still don’t understand Windows PowerShell. What is it good for?” she queried.
“Suppose you are a network administrator and you…”
“But I am not a network administrator,” she hastily interrupted.
“Yeah, that is right,” I admitted, trying another tack. “Suppose you have several copies of a program open, and you want to close all of them at the same time. What would you do,” I asked.
“Well, I would go to each program, and click the ‘X’ in the upper right hand corner,” she said.
“Exactly. If you had 10 programs open, that would be 10 clicks. And for each one, you would have to wait until the program came to the foreground so that you could click the ‘x’,” I said.
“Well, I guess I could also use Task Manager to kill the processes,” she added.
“That is true, but unfortunately, as you can see here, Task Manager does not allow you to select several programs and kill the processes with one click,” I added while opening Task Manager (as shown in the following image).
“So you are still stuck with selecting the process, and then navigating down to the End Process button to kill the process. So there is still a lot of extra work. Using the Stop-Process cmdlet, you can stop all of the processes at once. Here, let me show you,” I said pausing for things to sink in. She made the requisite listening noises, so I continued.
“In your Windows PowerShell console, type Stop-P and press TAB. Windows PowerShell will complete the Stop-Process cmdlet name for you. Now, you need to tell Windows PowerShell that you are going to give it a process name. Type –n and press TAB to complete the –name parameter. Now all you need to do is supply the name of the process to stop. In this example, use notepad. The command, my dear Scripting Wife, is therefore Stop-Process –name notepad. You can see it in your Windows PowerShell console,” I said (the following image shows the command).
“Did it work? I don’t see anything,” she commented in a rather confused voice.
“Are all the copies of Notepad gone? Let’s use Task Manager to see,” I suggested. “Yep, they are all gone.” (See the following image as proof.)
“As you can see using the Stop-Process cmdlet to stop multiple copies of the same process can be extremely efficient. But you can also use it to stop processes that are not from the same program,” I said.
“Now, Scripting Wife, if you will type Start-P and press TAB, tab completion will fill out the rest of the Start-Process cmdlet name. Use the Start-Process cmdlet to start an instance of Notepad. Press the up arrow and change notepad to calc. Your commands thus far are Start-Process Notepad and Start-Process Calc,” I said.
“Do you see Notepad and Calculator running?” I asked.
“Yes,” she replied.
“Cool, now we will stop both Calculator and Notepad in a single command. Type Stop-p and press TAB,” I said.
“Ok, now what?” she asked.
“After you have typed Stop-p and pressed TAB, type –n and press TAB. Tab completion will complete the –name parameter. How are you doing?” I inquired.
“I am a simple user, not simple,” she replied acidly. “Just get on with it. I don’t have all day.”
“Okay, after the –name parameter, type notepad, calc. Press ENTER when you are finished,” I said.
“Did I do it right,” she asked, turning her laptop around so I could see it. The following image shows what I saw on her laptop.
“Indeed you did. Now why don’t you go play with that, and see what else you can discover about those two cmdlets? I need to get back to work on this script I am working on for next week,” I said.
Weekend Scripter will continue tomorrow as the Scripting Wife attempts to…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