PowerShell: convert array to html table












0














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










share|improve this question
























  • 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.
    – LotPings
    Nov 12 '18 at 19:00










  • @LotPings let me rephrase the question
    – TiredOfProgramming
    Nov 12 '18 at 19:03
















0














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










share|improve this question
























  • 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.
    – LotPings
    Nov 12 '18 at 19:00










  • @LotPings let me rephrase the question
    – TiredOfProgramming
    Nov 12 '18 at 19:03














0












0








0







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










share|improve this question















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







html powershell converters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 19:23

























asked Nov 12 '18 at 18:43









TiredOfProgramming

347214




347214












  • 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.
    – LotPings
    Nov 12 '18 at 19:00










  • @LotPings let me rephrase the question
    – TiredOfProgramming
    Nov 12 '18 at 19:03


















  • 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.
    – LotPings
    Nov 12 '18 at 19:00










  • @LotPings let me rephrase the question
    – TiredOfProgramming
    Nov 12 '18 at 19:03
















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.
– LotPings
Nov 12 '18 at 19:00




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.
– LotPings
Nov 12 '18 at 19:00












@LotPings let me rephrase the question
– TiredOfProgramming
Nov 12 '18 at 19:03




@LotPings let me rephrase the question
– TiredOfProgramming
Nov 12 '18 at 19:03












2 Answers
2






active

oldest

votes


















1














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?






share|improve this answer





























    0














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





    share|improve this answer





















    • This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
      – TiredOfProgramming
      Nov 12 '18 at 19:24











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268257%2fpowershell-convert-array-to-html-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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?






    share|improve this answer


























      1














      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?






      share|improve this answer
























        1












        1








        1






        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?






        share|improve this answer












        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?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 19:19









        LotPings

        17.9k61532




        17.9k61532

























            0














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





            share|improve this answer





















            • This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
              – TiredOfProgramming
              Nov 12 '18 at 19:24
















            0














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





            share|improve this answer





















            • This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
              – TiredOfProgramming
              Nov 12 '18 at 19:24














            0












            0








            0






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





            share|improve this answer












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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 '18 at 19:04









            Ammar Iqbal

            314




            314












            • This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
              – TiredOfProgramming
              Nov 12 '18 at 19:24


















            • This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
              – TiredOfProgramming
              Nov 12 '18 at 19:24
















            This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
            – TiredOfProgramming
            Nov 12 '18 at 19:24




            This is not related to my question. I rather have more complex hash table. But thanks for taking time and answering the question
            – TiredOfProgramming
            Nov 12 '18 at 19:24


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268257%2fpowershell-convert-array-to-html-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings