Learn How to Create Custom Column Heads for PowerShell Out-GridView


Summary: The Scripting Wife learns how to create custom column headings for the Windows PowerShell Out-GridView cmdlet.

Weekend Scripter

Microsoft Scripting Guy, Ed Wilson, here. It is a typical weekend for me. I am up early, sipping a cup of English Breakfast tea, and watching a pot of Irish steel-cut oats simmer on the stove. I am glancing over several books, trying to decide upon which one I will spend the day with. I am leaning toward Shadow Divers, which is a book about a group of scuba divers in New Jersey.

I get up to stir the oats, and when I return to the table, I notice that I have a guest.

“Good morning,” I said cheerily. “What brings you out this early on a weekend morning?”

“Mumrf! Rrrrrr,” the Scripting Wife muttered.

“Would you like a bowl of oats?” I pleasantly asked.

“Do I look like a horse?” she quizzed not really expecting an answer.

“You really are not a morning person are you?” I asked not expecting an answer either.

“You know me better than that,” she threatened.

I got up, and eased over to the stove. I stirred the oats for a few seconds.

“Still too soupy,” I said to myself.

“Would you like a cup of tea, my dear?” I offered.

“What makes you think I want to drink HOT tea?” she asked. “You can get me a Coke if you want to do something to make me happy.”

“OK,” I said as I headed to the refrigerator. “I have an idea.”

“Stop the presses,” she said with pretentious seriousness.

“It came to me yesterday, when I was working on the script to report large files,” I said.

“And,” she said slowly indicating interest.

“You need to know how to add custom column headings to an Out-GridView,” I said.

“I already know how to do that. I read your blog yesterday,” she stated flatly.

“I see. Well show me what you have,” I said.

“All I need to do is to take the Get-FileSizes.ps1 script from yesterday, and add a pipe to the Out-Gridview cmdlet at the end of the script,” she announced.

“Uh huh. And that will work?” I asked.

“I am not certain because I have not done it before, but it should work,” she said with a little less confidence in her voice than she had previously exhibited. “You said to not be afraid to try things, so here goes.”

The Scripting Wife opened up the Get-FileSizes.ps1 script in the Windows PowerShell ISE. She went to the end of the script and added a pipeline character and the Out-GridView cmdlet. As shown in the following image, this did not work properly.

Image of code

“Well it nearly worked,” she said in a sulky voice.

“You were definitely on the right track,” I said. “You are learning a lot.”

“Are you making fun of me?” she asked.

“No. Not at all. Yesterday, you saw in my blog that I talked about using a hash table to create custom column heads for the Format-Table cmdlet. You surmised that you could pipe that to Out-GridView and it would provide you with the custom column headings,” I stated. “It is perfectly logical.”

“If it is so right, then why is it producing errors?” she asked.

“You mean if it is so right, why is it so wrong?” I smiled. “That sounds like the name of a country-western song. The problem is that the Format-Table cmdlet changes the object, and it should be the last thing in a pipeline.”

“I see,” she said with a voice that stated she really did not see.

“What you need to do, is change out Format-Table with the Select-Object cmdlet. Copy the Get-FileSizes.ps1 script into a new script and make the change. It will be simple to do,” I suggested. “Oh, and make sure you delete that Out-Gridview from the end of the Get-FileSizes.ps1 script before you mess it up.”

“So you are saying that all I need to do is to replace the Format-Table cmdlet with the Select-Object cmdlet, and it will work,” she said echoing disbelief.

“Yes ma’am,” I said.

The Scripting Wife copied the Get-FileSizes.ps1 script and pasted it into a new script. She then changed Format-Table to Select-Object. She also removed the AutoSize and Wrap switches that are used by the Format-Table cmdlet. The resulting code is shown here.


Get-ChildItem -path $home -ErrorAction silentlycontinue -Recurse |

Sort-Object -Property length -Descending |

Select-Object -property `

@{Label=”Last access”;Expression={($_.lastwritetime).ToshortDateString()}},

@{label=”size in megabytes”;Expression={“{0:N2}” -f ($_.Length / 1MB)}},

fullname | out-GridView

The Windows PowerShell ISE is shown here.

Image of PowerShell ISE

The Grid View output with the custom column headings is shown here.

Image of Grid View output

“Wow! That is cool,” she said. “But that also means I need to learn how to do hash tables better, don’t I?”

“Yes, I guess it does,” I said dreading the day I had to do that.

Without my noticing, she had slipped from the table, and spooned out my oats into a bowl. I looked up, nodded my thanks, and turned to the book I had selected for the day. As I glanced at the authors note, it began thus…“A few years ago, a friend told me a remarkable story. Two recreational scuba divers had recently discovered…” Ah yes, a sea epic. It should be fun.

Join me tomorrow as we continue to work our way through the 2011 Scripting Games Study Guide.

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

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon