How can I generate an array of PSCustomObjects from an array of hashes in Powershell v3? -
suppose have array of hashes in powershell v3:
> $hashes = 1..5 | foreach { @{name="item $_"; value=$_}} i can convert single hash pscustomobject this:
> $co = [pscustomobject] $hashes[0] > $co | ft -autosize value name ----- ---- 1 item 1 > $co.gettype() ispublic isserial name basetype -------- -------- ---- -------- true false pscustomobject system.object so far, good. problem occurs when try convert entire hash array pscustomobjects in pipeline:
> ($hashes | foreach { [pscustomobject] $_})[0].gettype() ispublic isserial name basetype -------- -------- ---- -------- true true hashtable system.object as see, i'm getting array of hashtable objects, not pscustomobjects. why different behavior, , how can accomplish want?
thanks.
i have yet figure out exact cause (everyone still learning something) issue related $_ pipe element somehow. can make code work if force cast of $_
$hashes = 1..5 | foreach { @{name="item $_"; value=$_}} $hashes | %{([pscustomobject][hashtable]$_)} output
value name ----- ---- 1 item 1 2 item 2 3 item 3 4 item 4 5 item 5 something curious
i didn't name , value, while testing (that literal hash table had headers , found confusing while testing), changed later data , output differs. post curious. hard show results in post.
name data ---- ---- item 1 1 item 2 2 item 3 3 item 4 4 item 5 5