Summary: The Scripting Wife learns from the Microsoft Scripting Guy about formatting Windows PowerShell output with Format-List.
It is a Monday morning much like any one of hundreds of previous Mondays. The celestial orb has yet to make an appearance today, but to be fair; I have not given it much of a chance. The virtual hands on my digital clock point to “Dude, it is still very early.” But with the 2011 Scripting Games rapidly approaching, the number of meetings I must attend each day also increase. The need to work on the Scripting Games and the plethora of meetings then are the reasons for the early morning wakeup call that left me sitting quietly alone at the dining room table, perusing my calendar on my Windows 7 Smart Phone while I wait for my Irish steel-cut oats to simmer. I am deep in thought as I contemplate my day over a cup of English Breakfast tea when a voice cuts the early morning silence like a weed whacker cleaning a poorly maintained garden path.
“Well there was no reason to set the alarm clock so early if you are just going to sit in the kitchen all day,” the Scripting Wife chastised.
“I am contemplating my day,” I replied in a voice that pleaded for understanding.
“You do not need to contemplate the day; you need to seize the day. I have things to do that require your input and you are holding me up,” she admonished.
“But it is still oh dark thirty,” I pleaded, “and my steel cut oats are simmering, and my tea is steeping.”
“Yeah, and next thing I know you will be misquoting John Paul Jones by saying you have not yet begun to eat,” she sarcastically added.
“That is pretty clever,” I added hopefully in a vain attempt to placate the recently awakened Scripting Wife.
“You have your laptop there, why don’t you turn it around and make yourself useful. I need you to teach me about formatting output. I see from the 2011 Scripting Games Study Guide, it is going to be on this year’s scripting games,” she said.
“Oh, I did not know you were going to enter again this year,” I queried.
“Well, it was fun, I learned a lot, and it was actually pretty cool,” she said. “Of course, I am not really entering in terms of competing for prizes. I am just doing it for fun.”
“So what’s your problem with formatting output?”
“What do you mean, “It’s broken.”
“Here, I will show you,” she said as she slid the laptop over and opened Windows PowerShell by clicking the icon on the task bar.
After just a few seconds of clickety-clickety-click, she exhaled triumphantly and exclaimed, “See it gives me a bogus error.”
“Well, my dear, first of all, the error is not bogus. It says that format-output is not recognized. In fact, the error tells you that Windows PowerShell does not recognize format-output as a cmdlet, function, script, or program—which is basically everything that Windows PowerShell can execute.”
“So why do you want me to learn something that does not work?”
“When I talk about formatting output, I mean that you will format the output from another cmdlet. You will use three different formatting cmdlets in Windows PowerShell. To find the format cmdlets, use the Get-Command cmdlet, and use the Verb parameter to look for cmdlets that use the format verb.”
The Scripting Wife thought for a few seconds and typed Get-c and then she pressed the <tab> key a couple times until Get-Command appeared in the Windows PowerShell console. She then typed –v and pressed the Tab key once. She then pressed space once, typed format, and pressed ENTER. The exact keystrokes the Scripting Wife typed are listed here (<tab> is the Tab key, <space> is the Spacebar, <enter> is the Enter (or Return) key).
The command as it appears in the Windows PowerShell console along with its associated output is shown in the following image.
“So what is Format-Custom,” the Scripting Wife asked.
“It is used to create a customized view of the output of a command. It is something that you would never do,” I said.
“So Format-Custom is too hard for me to use, huh?”
“No, of course not. It is just an advanced cmdlet that requires you to create custom XML files and to update the type data in Windows PowerShell. Most Windows PowerShell scripters can go their entire life and never need to use this cmdlet,” I said.
“OK. So I am guessing that the Format-List cmdlet formats a list. Right?”
“Yes, that is correct. The best way to understand the Format-List cmdlet is to compare it to other things. So, why don’t you get process information about a process named WinLogon,” I suggested.
“There you go again, trying to be Super Scripting Guy or something. How am I supposed to know how to get a process…oh wait a minute,” she said as she began to type, “Never mind. I got it.”
I looked over at the Windows PowerShell console, and sure enough, she had gotten it. The command and results appear in the following image.
The Scripting Wife had typed the command Get-Process WinLogon. The actual key strokes she used are shown here. (The command is not case sensitive, and therefore she could have used a lowercase g, p, and w if she had wished to do so.)
“So, my dear, you can see that the default display of the Get-Process cmdlet is a table. You can confirm this by using the Get-Process cmdlet to retrieve information about all of the processes,” I said.
She used the Up Arrow to retrieve her previous Get-Process WinLogon command, and then she used the Backspace key to remove the word WinLogon, and she pressed ENTER. She was rewarded with a screen-filling table of process information shown in the following image.
“Now compare that output with the output of pipelining your winlogon process information to the Format-List cmdlet,” I suggested.
She duly pressed the Up Arrow twice to retrieve the Get-Process WinLogon command, and at the end of her command she typed the Pipe symbol (above the Backslash character on her keyboard), and then she typed the Format-List command. The exact keystrokes that she typed are shown here (keep in mind, that depending on what other commands you may have typed, the results of <up arrow> might be different.)
<up arrow><up arrow><space>|<space>Format-l<tab><enter>
The command and the associated output are shown in the following image.
As shown in the previous image, the command that was constructed is Get-Process WinLogon | Format-List.
“Well that is pretty cool, I guess. But what happened to all the information,” she asked.
“When you did not choose any properties, you got the default list view for your process,” I explained.
“But I did not choose any properties because you did not tell me to,” she began.
“And that is just fine and dandy.”
“It may be fine, but it is not dandy,” she chided. “I want to see all the properties.”
“OK. You asked for it. Use the up arrow to retrieve your last command and type a * at the end of your command,” I explained.
It took only a three key strokes—a clickety-clickety-click—to alter the command. The exact key strokes the Scripting Wife typed appear here.
The revised command, Get-Process WinLogon | Format-List * and its associated output appear in the following image.
“OK, so you think you are pretty smart don’t you,” the Scripting Wife exclaimed.
“Actually, it is more common to refer to guys as handsome, not pretty—but yes, I am smart,” I confidently replied.
“How am I supposed to read all that stuff?”
“You said you wanted to see all the information.”
“Well, why is half the information missing?”
“I would question your math skills, but I know you used to do accounting. There are a few property values that are not displayed, but it is nowhere near half. The reason varies. For example, some of the properties such as WindowTitle do not apply to the WinLogon process. Other values such as MainModule, MaxWorkingSet, MinWorkingSet and some of the others require rights that you do not have as an ordinary user.”
“But you are logged onto your computer…”
“Yes, and my normal account does not have administrator rights, and I did not start the Windows PowerShell console with administrator rights. Remember last year when you needed to change the script execution policy?”
“Yes, I had to get you to log on as an administrator.”
“Yep. It is safer like that. What you need to do is to choose the properties that you want to display.”
“But that will be a lot of typing,” she complained.
“You can use wildcard characters. If the wildcard matches one property, it is displayed. If it matches more than one, then the results from all matching properties are displayed. Go ahead and try it out by using the letter r followed by the star (*) symbol. The star symbol, as you might recall, means that we are looking for any number of any characters.”
She pressed the up arrow to retrieve her previous command, and typed the letter r in front of the * symbol. The exact key strokes she typed are shown here.
<up arrow><left arrow>r<enter>
The output this time was significantly shorter. It is shown here.
PS C:\> Get-Process winlogon | Format-List r*
Responding : True
Just then the Scripting Wife jumped up and ran to the kitchen. She came back in a few seconds shaking her head.
“Dude, you are going to need to make yourself a bowl of cereal or something. You just burned your steel-cut oats.”
It is a Monday morning much like any one of hundreds of previous Mondays. I am sitting at the dining room table, munching on a bowl of soggy cereal, and contemplating the day ahead of me.
Tune in tomorrow when the Scripting Wife will experiment with the Format-Table cmdlet.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy