Format PowerShell Output with an Easy-to-Use Table


Summary: The Scripting Wife learns how to format Windows PowerShell output with the easy-to-use Format-Table cmdlet.

Microsoft Scripting Guy, Ed Wilson, here. I wonder how you might describe the perfect day? For me, it consists of receiving feedback via the email alias or on Twitter to the effect that something I have done has made someone’s life easier, or inspired them to learn about Windows PowerShell. Follow that with a chance to talk to a user’s group (preferably in person, but Live Meeting works as well) and the day is well on its way to being perfect. See the tweet in the following image for the kind of day this is shaping up to be.

Image of tweet

It is midafternoon in Charlotte, North Carolina, and as I implied earlier, my day is tracking along nicely to be recorded in the “perfect” category come midnight. As I was heading to the kitchen to snag a couple of Anzac biscuits and to prepare a pot of Earl Grey tea, the Scripting Wife accosted me.

“You know if you eat up all of those Anzac biscuits, you will have a hard time replenishing the jar—you are on your last bag,” she said.

“Dude, dude, dude—or better yet, dudette,” I inanely replied.

“Well as long as you are not doing anything, why don’t you pop over here, and help me with this last formatting cmdlet, Format-Table,” she suggested.

“Hmm, I was not aware that making a pot of tea and getting a biscuit was doing nothing; but sure, why not? I need to let the tea steep anyway,” I said.

Note: this is the third article in which the Scripting Wife learns about formatting output. On Monday, she learned how to work with the Format-List cmdlet. On Tuesday, she moved on to exploring the Format-Wide cmdlet, and today she will work with Format-Table.

“For the last two days, you have been boring me with Get-Process. Why don’t you show me something different today,” the Scripting Wife suggested.

“Why soowently (certainly),” I said in my best Three Stooges imitation.

“Can it Script Monkey! You know I can’t stand those guys,” she said with mock animosity.

“But they are great, classics, veritable philosophers,” I asserted.

“Come on, I don’t have all day. Tell me about Format-Table. I already have my Windows PowerShell console open,” she said.

“OK, get information about services and pipe the results to a table. Choose the Name and Status properties to display in your table,” I said.

She thought a little bit, and began typing. She came up with the command shown here.

Get-Service | Format-Table name, status

But she did not have to type all of that into her Windows PowerShell console because she used Tab Completion to reduce the amount of typing. What she actually typed appears here (I use <tab> for the Tab key, <space> for the Space key, and <enter> for the Enter or Return key.)

Get-s<tab><space>|<space>Format-t<tab><space> name,status<enter>

The resulting command and the associated output are shown in the following image.

Image of command output

“Well that is kind of cool, but what happens if I reverse the order of the properties that I chose,” she asked.

“The best way to find out is to just do it,” I said.

She used the Up Arrow, and the Backspace key to edit the line until it looked like the following.

Get-Service | Format-Table status,name

The command and the associated output are shown in the following image.

Image of command output

“That is ugly! What happened,” she asked.

“Who knows? One thing to keep in mind is that when you are using the Format-Table cmdlet, Windows PowerShell takes a real quick look at the data that is coming across the pipeline, and it makes a guess as to where to place the columns. Most of the time it is acceptable, occasionally it is not, but it is never optimal,” I explained.

“Huh? For someone who likes the Three Stooges, and is supposed to be a writer, you certainly have trouble avoiding geek speak don’t you,” she whined.

“OK, point taken. Suppose you were going to lay out information in three columns on a sheet of paper. What is the one thing you need to know?” I asked.

“I guess I would need to know the length of the longest line that I would store in each column. That way, I could figure out the size of each column,” she mused.

“You are absolutely right. How would you go about finding the longest line?” I asked.

“That is easy. I would have to go through each line that I was going to put in the table, and measure the length of it. I would then need to keep track of what the greatest number was to that point,” she replied.

“You are once again correct. Now what Windows PowerShell does is instead of going through the entire collection of content, it looks at only a little bit of the data, and then makes a guess,” I said.

“Well, I guess you could do that,” she said, “But I guess that could also get you in trouble later on.”

“Yep. But it is really fast. If you are concerned about making sure that you have a good display output to the Windows PowerShell console, you can use the AutoSize parameter. But keep in mind, it can really slow things down if you have a lot of data, because it causes Windows PowerShell to do exactly what you suggested earlier; that is, it measures the length of each line that will go into the table, and it figures out the best size,” I said. “Go ahead and try it.”

The Scripting Wife pressed the Up Arrow once, and typed –a, and then tabbed over before pressing ENTER. The exact key strokes she typed are here.

<up arrow><space>-a<tab><enter>

The command and the associated output are shown in the following image.

Image of command output

“As you can see, of the output created by the three commands, the one produced by AutoSize is the best. The one disadvantage is that it can take a long time before anything displays on the screen if there is a large amount of data to troll.”

“OK, I got it. This output is not so interesting. I am only interested in services that are running, and I have no idea what an ALG service is,” she said.

“The first thing you need to do is see what properties are available that would be better for helping you identify what service you are looking at. Use the gsv alias to see if something there will help you,” I said.

She typed gsv into the Windows PowerShell console and pressed ENTER. The command and the associated output are shown in the following graphic.

Image of command output

“Based on that output, it looks like you want to choose the display name. You probably also want the actual name of the service because some applications need it. Go ahead and choose Name and DisplayName in your query,” I said.

“OK,” she said.

The Scripting Wife used the Up Arrow a couple of times, and then edited the Format-Table command until it looked like the one shown here.

Get-Service | Format-Table name,displayname –AutoSize

The command and the associated output are shown in the following image.

Image of command output

“But now I don’t know if the service is running or not,” she complained.

“Not to woooowy (worry),” I said sounding a bit like Curly (I thought). “We need to use the Where-Object cmdlet to filter out only the services that have a Status of running,” I said.

I continued, “To do this, retrieve your previous command, and move the cursor until it is between the Get-Service command and the Format-Table command. Now this part is real important, do not press Tab. If you do, it will erase the rest of your command. Type Where, open the curly brackets, use $_.Status –eq ‘running’, and close your curly brackets. Add an additional pipeline character and press ENTER,” I said.

“I thought you said I needed to use a Where-Object cmdlet. Why did you only have me type where,” she asked.

“Because where is an alias for Where-Object. The question mark is also an alias for Where-Object, but it can be confusing at first, so I had you type the longer alias, Where,” I said.

The complete command that she typed is shown here.

Get-Service | where {$_.status -eq ‘running’}|Format-Table name,displayname –AutoSize

The command and its associated output are shown in the following graphic.

Image of command output

“Well done,” I said.

“That is cool,” she replied. “What are you going to teach me tomorrow?”

“Tomorrow, we will talk about using the Grid View. It is pretty cool,” I said while I grabbed another Anzac biscuit and began to head back upstairs.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy