How to initialize an array of custom objects

后端 未结 9 2495
粉色の甜心
粉色の甜心 2020-12-13 23:44

First, as this leads to my question, I\'ll start by noting that I\'ve worked with XML a fair bit in PowerShell, and like how I can read data from XML files, quickly, into ar

9条回答
  •  孤街浪徒
    2020-12-14 00:29

    Here is a more concise version of the accepted answer which avoids repeating the NoteProperty identifiers and the [pscustomobject]-cast:

    $myItems =  ("Joe",32,"something about him"), ("Sue",29,"something about her")
                | ForEach-Object {[pscustomobject]@{name = $_[0]; age = $_[1]; info = $_[2]}}
    

    Result:

    > $myItems
    
    name           age         info
    ----           ---         ----
    Joe            32          something about him
    Sue            29          something about her
    

提交回复
热议问题