{"id":379,"date":"2008-02-20T11:00:02","date_gmt":"2008-02-20T19:00:02","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/02\/20\/how-can-i-write-a-powershell-function-that-outputs-a-table\/"},"modified":"2008-02-16T11:16:06","modified_gmt":"2008-02-16T19:16:06","slug":"how-can-i-write-a-powershell-function-that-outputs-a-table","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/02\/20\/how-can-i-write-a-powershell-function-that-outputs-a-table\/","title":{"rendered":"How can I write a PowerShell function that outputs a table?"},"content":{"rendered":"<p>This is a REALLY common question, and if you have any experience with VBScript, or Perl, or some similar scripting language, you&#8217;ll go down completely the wrong path. The thing to remember is that PowerShell <i>already knows how to format tables<\/i>! You just have to give it your data in a way it understands, so it can make a table out of it. So what does PowerShell&#8217;s Format-Table cmdlet know how to work with?<\/p>\n<p>Objects.<\/p>\n<p>So the trick is to format whatever data you&#8217;re working with as an object. In other words, let&#8217;s say you have four variables that contain your data: $data1, $data2, $data3, and $data4. You might be tempted to create a table by doing this:<\/p>\n<p>Write-Host $data1 $data2 $data3 $data4<\/p>\n<p>And so forth. You might start your script with a statement that writes out column headers, even. But that&#8217;s not the &quot;PowerShell way&quot; of doing things. Here&#8217;s what is:<\/p>\n<p><code>$out = new-object psobject<br \/>\n$out | add-member noteproperty ColumnA $data1<br \/>\n$out | add-member noteproperty ColumnB $data2<br \/>\n$out | add-member noteproperty ColumnC $data3<br \/>\n$out | add-member noteproperty ColumnD $data4<\/code><\/p>\n<p>This gives you an object, stored in $out, containing four properties: ColumnA, ColumnB, ColumnC, and ColumnD. Simply output that by using <b>Write-Output $out<\/b> and you&#8217;re done: PowerShell will happily format it as a table. Since it only has four properties, you don&#8217;t even have to use the Format-Table cmdlet &#8211; PowerShell will choose a table layout automatically.<\/p>\n<p>A bonus of this technique is that you can also use other cmdlets, like Export-CSV, ConvertTo-HTML, and more. By having your output data contained within an object, all of PowerShell&#8217;s various exporting and formatting options become possible, with no extra work on your part.<\/p>\n<p>Think of an &quot;object&quot; as a row in a table; each property of the object becomes a column of the table. Here&#8217;s an example function that accepts a computer name, makes up some data as output, and outputs the object. I&#8217;m reading in a bunch of computer names from a file, and sending each one to the function. The result is a single output table.<\/p>\n<p><code>function MakeData($name) {<br \/>\n&nbsp;$data1 = $name.ToLower()<br \/>\n&nbsp;$data2 = $name.ToUpper()<br \/>\n&nbsp;$data3 = $name<br \/>\n&nbsp;$out = new-object psobject<br \/>\n&nbsp;$out | add-member noteproperty LowerCase $data1<br \/>\n&nbsp;$out | add-member noteproperty UpperCase $data2<br \/>\n&nbsp;$out | add-member noteproperty Original $data3<br \/>\n&nbsp;write-output $out<br \/>\n}<br \/>\n$computers = get-content c:\\names.txt<br \/>\nforeach ($computer in $computers) {<br \/>\n&nbsp;makedata $computer<br \/>\n}<\/code><\/p>\n<p>I could actually have written the function to accept pipeline input directly, making this possible:<\/p>\n<p><code>gc c:\\names.txt | makedata<\/code><\/p>\n<p>But I didn&#8217;t, simply because the way I used is a bit easier to follow as a learning example. So, the moral: Don&#8217;t output text. Output objects.<\/p>\n<p>Thanks to <b>Jon<\/b> for sending this one in.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a REALLY common question, and if you have any experience with VBScript, or Perl, or some similar scripting language, you&#8217;ll go down completely the wrong path. The thing to remember is that PowerShell already knows how to format tables! You just have to give it your data in a way it understands, so [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,25],"tags":[86,85,82,83,28,84],"class_list":["post-379","post","type-post","status-publish","format-standard","hentry","category-ask-the-experts","category-windows-powershell","tag-format","tag-formatting","tag-objects","tag-output","tag-powershell","tag-table"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=379"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}