title: PowerShell: convert array to html table
body: <p>How would I convert simple array of hashes in PowerShell to HTML table?</p>
  
  <pre><code>$array = @{
  
  "Name" = "My Name"
  "Surname" = "My Surname"
  "Address" = "My Address"
  "DateOfBirth" = "My Date Of Birth"
  "Hobby" = "My Hobby"
   "Age" = "My Age" 
   }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/9aoS6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9aoS6.png" alt="enter image description here"></a></p>
  
  <p>And then just keep adding rows? Has anyone achieved this before? Below I will provide examples of that I've tried so far according to several online forums:</p>
  
  <pre><code>[System.Management.Automation.PSCustomObject]$array | ConvertTo-Html
  -Fragment
  </code></pre>
  
  <blockquote>
    <p>Cannot convert the "System.Collections.Hashtable" value of type
    "System.Collections.Hashtable" to type
    "System.Management.Automation.PSCustomObject". At line:0 char:0
    + [System.Management.Automation.PSCustomObject]$array | ConvertTo-Html  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : ConvertToFinalInvalidCastException</p>
  </blockquote>
  
  <pre><code>New-Object psobject -Property $array | ConvertTo-Html -Fragment
  </code></pre>
  
  <blockquote>
    <p>System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry</p>
  </blockquote>
  
  <pre><code>$array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -Fragment
  </code></pre>
  
  <blockquote>
    <p>System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry</p>
  </blockquote>
  
  <pre><code> $array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -as Table -Fragment
  </code></pre>
  
  <blockquote>
    <p>System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry</p>
  </blockquote>
  
  <pre><code>$array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -as Table -Fragment | Out-String
  </code></pre>
  
  <blockquote>
    <p>System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry
    System.Collections.DictionaryEntry System.Collections.DictionaryEntry</p>
  </blockquote>
  
  <pre><code>$table = $array.GetEnumerator() | ConvertTo-Html -Fragment -As Table
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/LRIqG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LRIqG.png" alt="enter image description here"></a></p>
  
  <pre><code>$table = $array.GetEnumerator() | select "Name", "Surname", "Address", "DateOfBirth", "Hobby", "Age" | ConvertTo-Html -Fragment -As Table
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/3XDMb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3XDMb.png" alt="enter image description here"></a></p>
  
  <p>As you can see, so many different approaches, and none of them led to success :-(</p>