TiredOfProgramming
TiredOfProgramming

Reputation: 855

PowerShell: convert array to html table

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 :-(

Upvotes: 3

Views: 13214

Answers (2)

user6811411
user6811411

Reputation:

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?

Upvotes: 7

Ammar Iqbal
Ammar Iqbal

Reputation: 51

'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>

Upvotes: 1

Related Questions