Microsoft Scripting Guy Ed Wilson here. It’s a road trip. The Scripting Wife and I are heading out of town to attend a Windows PowerShell user group meeting where I will be speaking about Windows PowerShell best practices. Because I wrote a book about Windows PowerShell Best Practices, I guess people assume I know something about the subject. User groups are way cool and a great way to learn about Windows PowerShell.
We have one that meets on our Redmond, Washington, campus that is simply called the Script Club. People bring scripts they are working on that they are having problems with, or perhaps they want a second opinion about their script, and they receive lots of constructive feedback about their scripts. Receiving feedback about a script you have written is a great way to learn different approaches to scripting because you have, presumably, thought about the problem at hand and arrived at what you thought was a good solution, and now you get to see how someone else would tackle the same situation.
Hmm, that sounds an awful lot like what the 2010 Scripting Games are about: You write a solution to a problem, get to see dozens of other people’s solutions, and get to see how an internationally renowned scripter would approach the problem. In addition, during the wrap-up, you will get to see a sampling of some of the more intriguing scripts that were submitted during the games. Way cool—I can hardly wait for the events to begin.
Out of the corner of my eye, I see the Scripting Wife typing away on her laptop. She brought her power adapter and could therefore be occupied for hours.
“The sound of silence. Cool,” I think to myself. Let me review my presentation for the user group. “Windows PowerShell 2.0 best practices are…” my mind drifts “Let’s see. Hmm…”
Try. Catch. Finally your errors,
PowereShell best practices.
“That kind of works,” I say to myself.“
“Are you okay?” the Scripting Wife asks with a grin in her voice.
“Yes. Why do you ask?”
“You look like you are in pain.”
“I am smiling,” I explain.
“Oh. Well stop it. You’re creeping me out. You look like a cross between Count Dracula, and Dr. Evil, and that is something I did not know was possible,” she said.
“Boooowaaaaaahaaaaaa I have come to check your script Boooowaaaaaahaaaa,” I portended.
“Speaking of scripts,” she mock pleaded.
“No. Not today. You are not ready. I am busy. I need to pay attention to driving and not your scripting,” I stated matter-of-factly.
“I won’t mess things up I promise,” she pantomimed.
“Okay. Go to Programs/Accessories/Windows PowerShell and click Windows PowerShell ISE,” I instructed.
“Nothing is happening,” she said.
“You have to, like, give it a second, like, maybe, three,” I said in my best valspeak.
“You sound like an idiot when you do that,” she said. “Okay, it’s up. Now what?”
“Okay. As you are looking at the Windows PowerShell ISE, you should see there are three panes. The top pane is called the Script pane because…”
“That is where you write your scripts,” she blurted out.
“Well, what do you call the bottom pane?”
“It sort of looks like a Windows PowerShell prompt, so I am going to guess that is where you type Windows PowerShell commands. So it is the Console pane,” she guessed.
“I would give that to you if this were a test. It is called the Command pane,”
“Why not cmdlet pane? That would be better,” she helpfully suggested.
“You might have something there. So what is the middle pane?”
“The pain pane,” she laughed.
“Not in your wildest dreams. It is the Output pane,” I corrected. “In your script pane, type Get-Process and then click the green triangle. Tell me what happens.”
“I get a process listing in the pain pane,” she giggled. “See.” (She showed me what is shown in the following image.)
“No I don’t see. I am trying to drive; but I think that truck driver beside us saw it and maybe that is why he is blowing his horn. Either that, or he wants his lane back,” I said. “In your command pane, type cls and press ENTER to clear the output.”
“Hey, that is pretty cool, Scripting Guy,” she enthused.
“Up in your Script pane, pipe the results of Get-Process to the Select-Object cmdlet and select the name property, please. When you are done typing, run the script by clicking the green triangle,” I instructed.
“Okay. Cool. It works just like it does on the Windows PowerShell console,” she said. (See the following image.)
“To save your script, all you need to do…”
“Is go to the File menu, and click Save while choosing a directory. No big deal,” she said.
“And what is the file extension for a Windows PowerShell script?” I asked.
“A .ps1 file. Which reminds me: When do we get to use .ps2 files?” she asked in earnest.
“You don’t. To preserve backward compatibility with Windows PowerShell 1.0 and to avoid confusion, there are no .ps2 files. But, my dear, that is a better question than you realize. Because Windows PowerShell 2.0 installs into the WindowsSystem32WindowsPowerShellv1.0 directory, I have received a number of e-mail messages in the scripter inbox with people saying the upgrade did not work because the v1.0 folder remained,” I said.
“Now don’t start getting geeky on me. I am not all that interested, just curious. I don’t want to see if you can name all of the default XML files that ship with Windows PowerShell 2.0 or anything like that. We are already married, so you can quit trying to impress me. It does not work anyway,” she laughed.
“Beautiful segue my dear. Speaking of ‘doesn’t work now,’ try to run your script,” I joked.
“You are right. It no longer works,” she said. “It gives me a bogus error.” (See the following image.)
“This is because Windows PowerShell does not allow scripts to run when it is first installed. Because you have never run a script on your computer, scripts will not run.”
“But I was running scripts earlier,” she pointed out perplexedly.
“Actually, you were running commands earlier—they just happened to be in the script pane. But you were not running a .ps1 file.”
“So how am I going to fix it?” she pleaded.
“You are not. You do not have enough rights to do that. You have to be an administrator, and I am not giving you the password. So you have two choices: You can go back to the Windows PowerShell console, open a new script, and just not save it, or play around in the command pane. We will fix it tomorrow when we get back home and I have some time to explain it to you,” I promised.
“Okay. I am wanting to learn about the registry anyway,” she said.
“Now you know why you do not have administrator rights on your laptop,” I chided.
“Why? Why exactly?” she said with a bit of harshness to her voice.
“Hmmm … Windows PowerShell 2.0 Best Practices are …” my mind drifts “Let’s see.…”
Posh Best Practices
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