3

How would I convert simple array of hashes in PowerShell to HTML table?

$array = @{

"Name" = "My Name"
"Surname" = "My Surname"
"Address" = "My Address"
"DateOfBirth" = "My Date Of Birth"
"Hobby" = "My Hobby"
 "Age" = "My Age" 
 }

enter image description here

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:

[System.Management.Automation.PSCustomObject]$array | ConvertTo-Html
-Fragment

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

New-Object psobject -Property $array | ConvertTo-Html -Fragment

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

$array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -Fragment

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

 $array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -as Table -Fragment

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

$array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -as Table -Fragment | Out-String

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

$table = $array.GetEnumerator() | ConvertTo-Html -Fragment -As Table

enter image description here

$table = $array.GetEnumerator() | select "Name", "Surname", "Address", "DateOfBirth", "Hobby", "Age" | ConvertTo-Html -Fragment -As Table

enter image description here

As you can see, so many different approaches, and none of them led to success :-(

2
  • Your initial How would I convert simple array and the error msg Cannot convert the "System.Collections.Hashtable" should tell you, where you got it wrong: @{} creates a hashtable not a simple arry. Commented Nov 12, 2018 at 19:00
  • @LotPings let me rephrase the question Commented Nov 12, 2018 at 19:03

2 Answers 2

7

You mean something like this?

$table = [PSCustomobject]$array| ConvertTo-Html -Fragment -As Table
$table

<table>
<colgroup><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>Name</th><th>Age</th><th>Surname</th><th>DateOfBirth</th><th>Hobby</th><th>Address</th></tr>
<tr><td>My Name</td><td>My Age</td><td>My Surname</td><td>My Date Of Birth</td><td>My Hobby</td><td>My Address</td></tr>
</table>

From what source do you want to add rows?

Sign up to request clarification or add additional context in comments.

Comments

1

'11','22','33' | ForEach{[PSCustomObject]@{'My Column Name'=$_}} | ConvertTo-HTML -Fragment -Property 'My Column Name'

Your output would then be:

<table>
<colgroup><col/></colgroup>
<tr><th>My Column Name</th></tr>
<tr><td>11</td></tr>
<tr><td>22</td></tr>
<tr><td>33</td></tr>
</table>

1 Comment

This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.