Learn about Windows PowerShell
Summary: The Scripting Wife learns to use Windows PowerShell to write to text files and to include the date in a log.
Microsoft Scripting Guy, Ed Wilson, is here. The other night the Scripting Wife and I went to the Charlotte IT Pro User Group meeting where I participated in a panel discussion about various Microsoft approaches to various problems. It was a very fun and educational experience. Anyway, before we left the meeting, I agreed to participate in their IT Pro appreciation day. It will be an intense all day training. I will be talking about why IT Pros need to learn Windows PowerShell. I believe the Scripting Wife even volunteered to assist with registration (or some other myriad of details that these types of all-day events generate).
Speaking of the Scripting Wife, I came downstairs to look for her, but I do not seem to find her. I can see where she has been—a book turned upside down to mark the page, a cup of hot chocolate still steaming, a plate full of Tim Tams and her Windows 7 phone. These clues all seem to indicate that she has not been gone for long, nor will it be an extended time before she returns. The bad thing is that with her abandoning her Windows 7 phone I cannot call her to ask her whereabouts—that may be the idea.
I had just picked up one of her Tim Tams and was getting ready to consume it when she suddenly materialized in the doorway.
“Don’t you dare eat all my Tim Tams,” she chastised.
“I did not know there were any Tim Tams left,” I remarked.
“That is why they still exist,” she countered, “You have the Anzac Biscuits, and I have the Tim Tams. That was the deal.” She is not fooled; she knows that I will share my Anzac Biscuits with her.
“Yes you are right. Perhaps I only wanted to see if it was still good,” I lamely offered.
“Perhaps you only wanted to be a sneak and eat up all my Tim Tams,” she corrected. “As long as you are here, why don’t you make yourself useful? I need to know about creating files.”
“OK. Why don’t you sit down at your computer and open the Windows PowerShell console,” I suggested. “To begin with, write the results from the Get-Process cmdlet to a text file by using two right redirection arrows. The right redirection arrow points to the right, and on your particular keyboard it appears on the bottom row of the keyboard above the period. Just type Get-Process and redirect the output to a file called myprocesses.txt. Use your scratch directory, the one called FSO which is off of drive C.”
The Scripting Wife thought for a second, and appeared to study her keyboard.
“So all I need to do is this?” and she began to type. The following are the exact keyboard sequences that she typed.
“Nothing happened,” she said.
“Exactly. Nothing returns when you use the redirection arrows to create a file. Now open it in Notepad.”
The Scripting Wife moved her hand to the mouse, and began navigating through the menus.
“Stop,” I said. “Put down that mouse. It is easier and faster to open Notepad and the file all at once. Do this, type the word notepad and follow it with the path to your file you just created.”
The Scripting Wife typed notepad and then began to type the path to the file.
“One thing to keep in mind is you can use Tab Expansion to fill in the path. It is much faster when you do that.”
“OK,” she said.
The following is exactly the command the Scripting Wife typed.
The image that follows illustrates the first command (which creates the file) and the second command (which opens the file).
The newly created text file is shown in the image that follows.
“By the way, it is not necessary to type the word notepad in front of the path to the text file because the default file association of a .txt file is with notepad.exe. This means that it will automatically execute if all you do is supply the path to the file,” I said. “Why don’t you go ahead and try it?”
The Scripting Wife uses the Up arrow to recall the previous command, and she erases the word notepad by using the backspace. The revised command is shown here.
“The double redirection arrow appends to a text file. This means if you run your command to write process information to a text file, it will add the new content to the bottom of the file. This is a handy feature when you want to add information to a text log file,” I said. “So why don’t you recall your previous command that writes process information to the text file, and run it again?”
The Scripting Wife used the Up arrow a couple of times until she recalled the line that is shown here. Then she pressed the ENTER key to run the command a second time.
Get-Process >> c:\fso\myprocesses.txt
Next she recalled the command to open the text file in Notepad. Her Windows PowerShell console is shown in the image that follows.
The revised text file is shown here.
“If you want to keep only one copy of information, instead of creating a continuous log, for example, you can use a single redirection arrow, instead of using two redirection arrows,” I said. “To make it easy to see the difference, change your Get-Process command that writes to a text file to Get-Service instead. Also, change the double redirection arrow to a single one.”
The Scripting Wife used the Up arrow a couple times until she had the Get-Process command on the command line. Then she edited the line so that it appears like the one here.
Get-service > c:\fso\myprocesses.txt
She then opened it in Notepad by recalling the notepad command that is shown here.
The modified text file is shown in the following image.
“Well, what do I do if I want to write the date and time that I get this information at the top of the file?” the Scripting Wife asked.
“That is a great question! I think the easiest way to do that is to make two commands. Remember that the semicolon is a command separator? All you need to do is to write the date to the file, and then append the process information to the same file. The key, here, is to first use the “overwrite” (the single redirection arrow) so that your date will appear on the top of the file. Then it is important to use the “append” (the double redirection arrow) to add the process information to the bottom of the file,” I instructed. “Go ahead and give it a try.”
The Scripting Wife thought for a little bit, and then she arrived at the command that is shown here.
get-date > c:\fso\myprocesses.txt; Get-Process >> c:\fso\myprocesses.txt
She then opened the file in Notepad. The text file is shown in the following image.
“What if I want to keep adding stuff to the log, and I do not want it to overwrite everything?” she asked.
“In that case, you change the first single redirection arrow to a double redirection arrow,” I said.
The Scripting Wife thought for a second, recalled the previous command by using the Up arrow, and modified it such that it is shown here.
get-date >> c:\fso\myprocesses.txt; Get-Process >> c:\fso\myprocesses.txt
The commands and associated output are shown in the image that follows.
“Thanks. Well, I am out of here. Stay out of my cookies,” she said.
“Huh? What,” I asked.
“Well you are going to be in an all-day conference call today, so there is no reason for me to hang around and be in your way. I have been planning a girl’s day out today for some time,” she said.
“Sounds expensive,” I hesitantly suggested.
“Not too bad, but you better hope the stock market goes up today just in case,” she smiled and evaporated before my very eyes. Sometimes I wonder …
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Narrative-wise: "sounds expensive" and "That is why they still exist" are my favorites. :)
PowerShell-wise: I would rather go with Out-File to avoid encoding issues, and issues with butchering long lines... :)
writing to files is really easy in powershell!
Using outup redirection is identical to the way cmd.exe handles it.
So we can have our screen dumped to files just by adding a ">" or ">>" sign followed by a filename! That's simple! ( though I sometimes would prefer having the comparison signs for comparsions, but we get used to this .... sooner or later )
Have a nice weekend!
PowerShell = Powerful as VBScript + Simple as Batch
Instead of calling notepad you could also use "Invoke-Item" to call the default app for whatever the file type...
First of all I want to say that I'm new at PowerShell but loving it!! And really enjoy reading the blogs here!
I was going to ask the simple question... Is there a difference/advantage over using this method or Add-Content cmdlet?
From the top, I notice two advantages by using the redirection arrows:
1. >> or > creates the file if it does not exist
2. "Get-Process | Add-Content -Path filename" adds the raw object (for lack of the correct terminology here, sorry) "System.Diagnostics.Process (powershell_ise)" and not the actual output you normally see in powershell by typing the Get-Process command.
So when would Add-Content be used advantageously?