Renam or Merge Array Properties

Good Morning from the UK.

 

I’ve got an Array in Powershell that I’ve managed to add to using my code. It looks like this:

Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 173
Length : 40
Value : 700
ID:erui5746

Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 248
Length : 36
Value : 800
ID:erui5745

Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 323
Length : 35
Value : 900
ID:erui5743

 

In my array which is called $arr1 I need the properties ‘Value’ and ‘ID’ to be under the same, new property called MyValue.

I’ve tried (foreach $a in $arr1){$arr2+=$a} to add the properties into a new array

But I still end up with ID and Value just in another array. I need to merge ID and Value properties into a single property.

 

Any help would be great

$arr2 = $arr1 | foreach {
    [PSCustomObject]@{
        Groups   = $_.Groups
        Success  = $_.Success
        Name     = $_.Name
        Captures = $_.Captures
        Index    = $_.Index
        Length   = $_.Length
        MyValue  = "$($_.Value)$($_.ID)"
    }   
}

Id would have been helpful to know how you created this array of hastables. And please format your code and sample data and console output as code using the code tags named “PRE”. Thanks.

@{
    Groups   = 0, 1
    Success  = $true
    Name     = 0
    Captures = @(0)
    Index    = 173
    Length   = 40
    Value    = 700
    ID       = 'eruis5746'
}, 
@{
    Groups   = 0, 1
    Success  = $true
    Name     = 0
    Captures = @(0)
    Index    = 248
    Length   = 36
    Value    = 800
    ID       = 'erui5745'
}, 
@{
    Groups   = 0, 1
    Success  = $True
    Name     = 0
    Captures = @(0)
    Index    = 323
    Length   = 35
    Value    = 900
    ID       = 'erui5743'
} | Select-Object -Property Groups,Success,Name,Captures,Index,Length,@{Name = 'MyValue'; Expression = {$_.Value.toString() + $_.ID.toString()}}

awesome, worked perfectly.

 

Thanks for your help!

I’m curious. Which approach do you mean? They should work both. :wink: