{"id":15401,"date":"2011-03-06T00:01:00","date_gmt":"2011-03-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/06\/learn-how-to-create-custom-column-heads-for-powershell-out-gridview\/"},"modified":"2011-03-06T00:01:00","modified_gmt":"2011-03-06T00:01:00","slug":"learn-how-to-create-custom-column-heads-for-powershell-out-gridview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-how-to-create-custom-column-heads-for-powershell-out-gridview\/","title":{"rendered":"Learn How to Create Custom Column Heads for PowerShell Out-GridView"},"content":{"rendered":"<p><b>Summary<\/b>: The Scripting Wife learns how to create custom column headings for the Windows PowerShell <strong>Out-GridView<\/strong> cmdlet.<\/p>\n<h3>Weekend Scripter <\/h3>\n<p>Microsoft Scripting Guy, Ed Wilson, here. It is a typical weekend for me. I am up early, sipping a cup of <a target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/English_Breakfast_tea\">English Breakfast tea<\/a>, and watching a pot of <a target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Irish_oatmeal\">Irish steel-cut oats<\/a> 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 <a target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Shadow_Divers\">Shadow Divers<\/a>, which is a book about a group of scuba divers in New Jersey. <\/p>\n<p>I get up to stir the oats, and when I return to the table, I notice that I have a guest.<\/p>\n<p>&ldquo;Good morning,&rdquo; I said cheerily. &ldquo;What brings you out this early on a weekend morning?&rdquo;<\/p>\n<p>&ldquo;Mumrf! Rrrrrr,&rdquo; the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\">Scripting Wife<\/a> muttered.<\/p>\n<p>&ldquo;Would you like a bowl of oats?&rdquo; I pleasantly asked.<\/p>\n<p>&ldquo;Do I look like a horse?&rdquo; she quizzed not really expecting an answer.<\/p>\n<p>&ldquo;You really are not a morning person are you?&rdquo; I asked not expecting an answer either.<\/p>\n<p>&ldquo;You know me better than that,&rdquo; she threatened.<\/p>\n<p>I got up, and eased over to the stove. I stirred the oats for a few seconds.<\/p>\n<p>&ldquo;Still too soupy,&rdquo; I said to myself. <\/p>\n<p>&ldquo;Would you like a cup of tea, my dear?&rdquo; I offered.<\/p>\n<p>&ldquo;What makes you think I want to drink HOT tea?&rdquo; she asked. &ldquo;You can get me a Coke if you want to do something to make me happy.&rdquo;<\/p>\n<p>&ldquo;OK,&rdquo; I said as I headed to the refrigerator. &ldquo;I have an idea.&rdquo;<\/p>\n<p>&ldquo;Stop the presses,&rdquo; she said with pretentious seriousness.<\/p>\n<p>&ldquo;It came to me yesterday, when I was working on the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/03\/05\/use-powershell-to-explore-disk-utilization-on-your-computer.aspx\">script to report large files<\/a>,&rdquo; I said.<\/p>\n<p>&ldquo;And,&rdquo; she said slowly indicating interest.<\/p>\n<p>&ldquo;You need to know how to add custom column headings to an <b>Out-GridView<\/b>,&rdquo; I said.<\/p>\n<p>&ldquo;I already know how to do that. I read your blog yesterday,&rdquo; she stated flatly.<\/p>\n<p>&ldquo;I see. Well show me what you have,&rdquo; I said.<\/p>\n<p>&ldquo;All I need to do is to take the Get-FileSizes.ps1 script from yesterday, and add a pipe to the <b>Out-Gridview<\/b> cmdlet at the end of the script,&rdquo; she announced.<\/p>\n<p>&ldquo;Uh huh. And that will work?&rdquo; I asked. <\/p>\n<p>&ldquo;I am not certain because I have not done it before, but it should work,&rdquo; she said with a little less confidence in her voice than she had previously exhibited. &ldquo;You said to not be afraid to try things, so here goes.&rdquo;<\/p>\n<p>The Scripting Wife opened up the Get-FileSizes.ps1 script in the <a target=\"_blank\" href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a> ISE. She went to the end of the script and added a <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipeline<\/a> character and the <strong>Out-GridView<\/strong> cmdlet. As shown in the following image, this did not work properly.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8203.WES-3-6-11-01_271EC2AF.jpg\"><img decoding=\"async\" height=\"456\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4863.WES-3-6-11-01_thumb_157EB217.jpg\" alt=\"Image of code \" border=\"0\" title=\"Image of code \" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;Well it nearly worked,&rdquo; she said in a sulky voice. <\/p>\n<p>&ldquo;You were definitely on the right track,&rdquo; I said. &ldquo;You are learning a lot.&rdquo;<\/p>\n<p>&ldquo;Are you making fun of me?&rdquo; she asked.<\/p>\n<p>&ldquo;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 <b>Format-Table<\/b> cmdlet. You surmised that you could pipe that to <b>Out-GridView<\/b> and it would provide you with the custom column headings,&rdquo; I stated. &ldquo;It is perfectly logical.&rdquo;<\/p>\n<p>&ldquo;If it is so right, then why is it producing errors?&rdquo; she asked.<\/p>\n<p>&ldquo;You mean if it is so right, why is it so wrong?&rdquo; I smiled. &ldquo;That sounds like the name of a country-western song. The problem is that the <b>Format-Table<\/b> cmdlet changes the object, and it should be the last thing in a pipeline.&rdquo;<\/p>\n<p>&ldquo;I see,&rdquo; she said with a voice that stated she really did not see.<\/p>\n<p>&ldquo;What you need to do, is change out <b>Format-Table<\/b> with the <b>Select-Object<\/b> cmdlet. Copy the Get-FileSizes.ps1 script into a new script and make the change. It will be simple to do,&rdquo; I suggested. &ldquo;Oh, and make sure you delete that <b>Out-Gridview<\/b> from the end of the Get-FileSizes.ps1 script before you mess it up.&rdquo;<\/p>\n<p>&ldquo;So you are saying that all I need to do is to replace the <b>Format-Table<\/b> cmdlet with the <b>Select-Object cmdlet<\/b>, and it will work,&rdquo; she said echoing disbelief.<\/p>\n<p>&ldquo;Yes ma&rsquo;am,&rdquo; I said.<\/p>\n<p>The Scripting Wife copied the Get-FileSizes.ps1 script and pasted it into a new script. She then changed <b>Format-Table<\/b> to <b>Select-Object<\/b>. She also removed the <i>AutoSize<\/i> and <i>Wrap<\/i> switches that are used by the <b>Format-Table<\/b> cmdlet. The resulting code is shown here.<\/p>\n<blockquote>\n<p>Get-FileSizesOutGrid.ps1<\/p>\n<p>Get-ChildItem -path $home -ErrorAction silentlycontinue -Recurse | <\/p>\n<p>Sort-Object -Property length -Descending | <\/p>\n<p>Select-Object -property `<\/p>\n<p>@{Label=&#8221;Last access&#8221;;Expression={($_.lastwritetime).ToshortDateString()}},<\/p>\n<p>@{label=&#8221;size in megabytes&#8221;;Expression={&#8220;{0:N2}&#8221; -f ($_.Length \/ 1MB)}}, <\/p>\n<p>fullname | out-GridView<\/p>\n<\/blockquote>\n<p>The Windows PowerShell ISE is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2248.WES-3-6-11-02_4F214C01.jpg\"><img decoding=\"async\" height=\"456\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4073.WES-3-6-11-02_thumb_66001D7D.jpg\" alt=\"Image of PowerShell ISE\" border=\"0\" title=\"Image of PowerShell ISE\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The Grid View output with the custom column headings is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2742.WES-3-6-11-03_55A4A5C4.jpg\"><img decoding=\"async\" height=\"344\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7534.WES-3-6-11-03_thumb_2123D37C.jpg\" alt=\"Image of Grid View output\" border=\"0\" title=\"Image of Grid View output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;Wow! That is cool,&rdquo; she said. &ldquo;But that also means I need to learn how to do hash tables better, don&rsquo;t I?&rdquo;<\/p>\n<p>&ldquo;Yes, I guess it does,&rdquo; I said dreading the day I had to do that. <\/p>\n<p>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&hellip;&ldquo;A few years ago, a friend told me a remarkable story. Two recreational scuba divers had recently discovered&hellip;&rdquo; Ah yes, a sea epic. It should be fun. <\/p>\n<p>Join me tomorrow as we continue to work our way through the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/02\/11\/2011-scripting-games-study-guide.aspx\">2011 Scripting Games Study Guide<\/a>. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25,3,4,74,61,45],"class_list":["post-15401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-scripting-wife","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15401"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}