Microsoft Scripting Guy Ed Wilson here. I love it when it rains. It sings of cleansing, freshness, new birth, and endless possibilities. Especially when it is a gentle spring rain like we are enjoying right now. It reminds me of when the Scripting Wife and I first went to Maui, Hawaii. We were staying near the beach, and every afternoon it would rain for about a half hour. We could just about set our watches by the rain showers. When the rain stopped, the sun came back out, and if we were lucky we were greeted with a rainbow. The following photo is one I took of one of those afternoon rainbows.
I enjoy sitting out on the front porch and watching the rain, depending of course on which way the wind is blowing. I do not enjoy sitting on the front porch and getting drenched, but I do enjoy the cool breeze and the sweet smell of spring rain. I was just about to lean back in the swing, close my eyes, and visualize tropical rainbows, when the Scripting Wife materialized in the swing seat next to me. She had her laptop with her and a look of concern upon her countenance, so I knew my daydreaming was rapidly drawing to a close.
“Isn’t it a lovely afternoon shower? If you close your eyes, and let your mind wander, it will remind you of Maui,” I offered.
“Can it, Script Monkey. You are not going to weasel out of teaching me. You see my laptop, and you know I am having a hard time learning Windows PowerShell. I want you to explain text files to me. I see they are on the review guide for the 2010 Scripting Games,” she said with a smile.
“It is Microsoft Scripting Guy, not Microsoft Script Monkey,” I said by way of clarification.
“Script Monkey, Script Monkey, Script Monkey,” she said mockingly.
“Now, do you really think that is a good way to ask for help?” I asked with exaggerated seriousness.
“Yes, I do.”
“How could you possibly come out here, call me ‘Script Monkey’ and expect me to help you?” I asked.
“Because I’m cute,” she parried.
She had a good point.
“Text files in Windows PowerShell are really easy,” I began. “You can redirect output and create a text file at the same time.”
“There you go again with geek speak,” she interrupted. “What do you mean by redirect output?”
“Redirection is taking the output from a command, and sending it somewhere else. For example, when you type Get-Process, the cmdlet displays a lot of information on the screen. If you would like to write that information to a text file, you can use the redirection operator, which is the greater than symbol, to cause the information to go to the file instead of being displayed on the screen. The cool thing is that the greater than symbol looks like it is funneling the output to the file. The command to write the process information to a text file would be Get-Process > c:fsoprocess.txt,” I said.
“Okay. Let me try it. I learn better by doing than by listening,” she said.
After a few seconds of typing, I heard her exclaim, “Dude, I hate this stuff!”
“What is wrong, my dear?” I asked with sincerity.
“Look. I got one of those bogus error messages,” she said, showing me what you see in the following image.
“Uh, my dear, that is not a bogus error message. It says that it cannot find part of the path c:fsoprocess.txt. Now which part of the file path do you think it is unable to find?”
“The folder,” she offered tentatively.
“That makes sense. Let’s see if the c:fso folder exists.”
The Scripting Wife reaches for the mouse.
“Stop! Let go of that mouse. We can use Windows PowerShell to test for the existence of the c:fso folder,” I said. “What do you think the name of a cmdlet would be that will test for the existence of the folder?”
“Test-Folder,” she stated.
“Close. It is Test-Path. You see a folder is also part of a path,” I explained. “Now type Test-Path c:fso and press ENTER.”
“It says false,” she said.
“That means the folder does not exist. So let’s create it. That sounds like fun, huh?”
“This could very well be the next Disney ride. That’s how much fun it sounds like.”
“Okay, then. Type md c:fso and press ENTER. You see md stands for make directory. It is actually an alias for a different command. You remember aliases? We talked about them a couple weeks ago.”
“No. You didn’t. You told me to read about them, and you pointed me to a Hey, Scripting Guy! article, but it really did not make too much sense to me,” she complained.
“Well, alrighty then. An alias is a shortcut name or a nickname for a longer cmdlet. Take for example the Get-Process cmdlet; it has an alias of GPS, which I remember as standing for G for Get, P for Proce, and S for s. Get-WmiObject has an alias of GWMI which I remember as…”
“I get it. G for Get and WMI for WMIObject. Is it case sensitive?”
“No. You can type GWMI or gwmi or gWmi or…”
“I don’t have all day. So you said that md is an alias and it stands for make directory. Correct?”
“Well, yes and no. I sort of lied to you. There is an old DOS command md that everyone called make directory. MD is actually an alias for a function called mkdir that is used to create folders,” I said.
“Oh, I see.”
“Whenever you want to see if something is an alias, use the Get-Alias cmdlet. For example, to see if md is an alias, you type Get-Alias md,” I added. “To see all of the aliases, type Get-Alias by itself.”
“Cool. Can I show you what I have typed so far,” she asked.
“Okay. Good. Let’s get back to creating a text file. Now that we have created the c:fso folder, we are ready to write process information to it. Press the up arrow a couple of times until you retrieve the Get-Process > C:fsoprocess.txt command and then press ENTER.”
“It did not do anything,” she said.
“Did it give you an error?”
“Then it probably worked. You can use the Test-Path cmdlet to see if the file exists. Type Test-Path c:fsoprocess.txt and press ENTER.”
“It says true,” she said.
“Then truly it exists,” I said. “Let’s open it up in Notepad and look at it. Type notepad c:fsoprocess.txt and press ENTER.”
“Groovy. It worked,” she said as she turned her monitor around so I could see.
“Sweet. Why don’t you go play around with that some more, and we will talk about this in more detail later,” I suggested.
“Good idea. Be careful and don’t get all wet and come tracking back in the house,” she said as she closed the door to the house behind her and faded from view.
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