Scripting Wife Learns to Format Wide Output with PowerShell
Summary: The Scripting Wife learns how to use the Format-Wide cmdlet in Windows PowerShell.
I imagine winter has nearly departed Charlotte, North Carolina and that spring will arrive soon. Yesterday, besides being rather long, turned out to be gorgeous outside—at least so it appeared from the window of my upstairs home office. The sky was a deep blue, with cumulous clouds that appeared as cotton balls to add depth and perspective to an otherwise seemingly endless sea of atmosphere. Springtime in Charlotte is one of the reasons that some people endure the oppressive heat and humidity of the long deep-south summers. Spring is something to look forward to; summer can be dreadful. I am hesitant to become too optimistic however, because I do remember snowstorms in March.
I decide to take my laptop to the front porch to take advantage of the cool morning air and our strategically placed porch swing. With my wireless network and my Windows 7 laptop, I can work from any comfortable chair in the house. I had just finished checking the email@example.com email, when the Scripting Wife appeared with a fresh baked cinnamon-raisin scone and a pot of English Breakfast tea.
“Good morning my little script monkey,” she oozed.
“Good morning to you Scripting Wife,” I hesitantly replied.
“How has your morning been?”
“Oh, things have been going great. I have already finished checking the email.”
“Well then, you will have time to show me more about formatting output,” she cheerfully said as she placed the scone and the teapot on the table beside the swing.
I handed her the laptop, and retrieved the scone and a cup of tea.
“You don’t have to bribe me you know. And it certainly is not your job to furnish me with tea and scones.”
“I know. I just happened across a new recipe for cinnamon-raisin scones, and while I was waiting for the scones to bake, I made myself a cup of hot chocolate. You got the leftover water,” she said with a smile.
“Well, thanks anyway. The scone is great. You should bookmark that site. Open the Windows PowerShell console.”
“OK, it is open. Now what?” she asked.
“Use the Get-Help cmdlet to find Help about the Format-Wide cmdlet.”
“Hmm…Get-Help Format-Wide,” she said quietly to herself as she typed.
The exact command she typed appears here (<enter> refers to the Enter key, or the Return key as it appears on some keyboards.)
The output from this command is shown in the following image.
“As you can see, the Format-Wide cmdlet is used to create a table that displays only one property of each object. In reality, I like to think of it as creating columns of information. By default it will create a two column display of a particular property. To understand this better, go ahead and try it. I want you to get process information, and display the name of each process,” I said.
“OK. Obviously, I will need to use Get-Process to get process information, and I am guessing I will use Format-Wide. To get information from Get-Process to Format-Wide, I will use the pipeline character. I will also need to tell Format-Wide which property to display,” she said just before she began to type.
I heard a clickety-clickety-clickety-click, so I leaned over to see what she had typed, just about the time she was getting ready to press the Enter key. Here is what she had typed.
The resulting command, Get-Process | Format-Wide name and its associated output is shown in the following image.
“That looks good. Now, change your command to only retrieve processes that begin with the letter s. Keep the same formatting command,” I instructed.
“That was easy,” she replied.
I looked over at the computer screen, and she indeed had the correct Get-Process s* | Format-Wide name command. Here is exactly what she typed.
<up arrow> <left arrow (until the insertion point is between the s from get-process and the bar character)><space>s*<space><enter>
The command and its associated output is shown in the following image.
“One of the cool things you can do with the Format-Wide cmdlet is define the number of columns. For example, you can use the Column parameter to display the output in three columns by providing the number 3 to the parameter Column. Go ahead and try it,” I said.
“That will be easy,” she said as she began typing.
I watched as she created the command. The first thing she did was use the Up Arrow to recall the previous command. Then she used the Space Bar to insert a space at the end of the command, typed –C and pressed Tab. Then she pressed Space, typed 3 and pressed ENTER. The exact keys she typed are listed here.
The command and associated output are shown in the following image.
“Now use the up arrow, and change the number of columns and watch as the display changes. Keep doing this until you notice that the process names begin to be partially obscured,” I said.
After about a minute or so, the Scripting Wife had created the output shown in the following image.
“If you would like to avoid a bunch of trial and error, use the AutoSize parameter instead of the Column parameter,” I said.
“Like this?” she asked.
I watched as she pressed the Up Arrow to retrieve the previous command, and then back-spaced to erase the –Column 7 parameter. She then added the AutoSize parameter. The exact keystrokes she typed are shown here.
<up arrow><backspace (until –column 7 is erased)>-a<tab><enter>
The resulting command and associated output is shown in the following image.
“Cool, I learned something new today. See you later, I am heading to the library. They sent me an email that my requested book is available,” the Scripting Wife said as she dashed to the front door.
And with that she was gone.
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