title: How to convert a collection of hashtables to a table within a file?
body: <p>We're trying to write an <code>array</code> of ordered <code>hashtables</code> to a file. While doing this we would like to to be aligned properly in a table format. A bit similar to <code>Format-Table</code> but differently presented.</p>
  <p>The code below works fine for the first case but not for the second one. In the second one we have a  property that is of type <code>array</code> and is incorrectly assessed. It feels a bit like we're reinventing the wheel here, is there a better solution?</p>
  <p>Usually the values will be of type <code>string</code>, <code>string[]</code> or <code>boolean</code>, but what if another type is passed? Thanks for your help.</p>
  <p><strong>Desired goals</strong></p>
  <pre class="lang-bsh prettyprint-override"><code>Describe 'ConvertTo-TextTableHC' {
      It 'should convert a collection of hashtable to a text table' {
          $testParameters = @(
              [ordered]@{
                  Name      = 'ScriptName'
                  Value     = 'Get printers'
                  Type      = 'String'
                  Mandatory = $true
              }
              [ordered]@{
                  Name      = 'PrinterName'
                  Value     = ''
                  Type      = 'String[]'
                  Mandatory = $false
              }
          )
          $actual = ConvertTo-TextTableHC -Name $testParameters
  
          $expected = @&quot;
  Name        Value        Type     Mandatory
  ScriptName  Get printers String   True
  PrinterName              String[] False
  &quot;@
          $actual | Should -Be $expected
      }
      It 'should convert a collection of hashtable to a text table' {
          $testParameters = @(
              [ordered]@{
                  Mandatory = $true
                  Name      = 'ScriptName'
                  Value     = 'Get printers'
                  Type      = 'String'
              }
              [ordered]@{
                  Mandatory = $false
                  Name      = 'OU'
                  Value     = @('ou1', 'ou2')
                  Type      = 'String[]'
              }
          )
          $actual = ConvertTo-TextTableHC -Name $testParameters
  
          $expected = @&quot;
  Mandatory Name        Value        Type
  True      ScriptName  Get printers String
  False     OU          ou1, ou2     String[]
  &quot;@
          $actual | Should -Be $expected
      } -Tag test
  }
  </code></pre>
  <p>This is the function:</p>
  <pre class="lang-bsh prettyprint-override"><code>Function ConvertTo-TextTableHC {
      [CmdletBinding()]
      Param (
          [Parameter(Mandatory)]
          [System.Collections.Specialized.OrderedDictionary[]]$Name,
          [int]$Spacing = 1
      )
  
      $result = [String[]]::new($Name.Count + 1)
  
      foreach ($prop in $($name[0].Keys)) {
          $rows = , $prop + $Name.$prop
          $maxChars = ($rows | Measure-Object -Maximum -Property Length).Maximum + $Spacing
  
          for ($i = 0; $i -lt $rows.Count; $i++) {
              $spaces = ' ' * (
                  $maxChars - 
                  ($rows[$i] | Measure-Object -Character).Characters
                  # fails on boolean values:
                  # ($rows[$i] | Measure-Object -Maximum -Property Length).Maximum
              )
              $result[$i] += '{0}{1}' -f $rows[$i], $spaces
          }
      }
  
      # remove spaces from last column
      $result =  $result.ForEach({$_.Trim()})
      $result -join &quot;`r`n&quot;
  }
  </code></pre>