Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

PSCustomObject and Select-Object Expandproperty

$
0
0

Hi there!

I created the following pscustomobject with a nested pscustomobject. Interestingly after Select-Object -Expandproperty the source object is changed. Do you have an answer on that?

# Create array for subobject object
$subobject = @()
$subobject += [pscustomobject]@{'FieldName'='Value1';'FieldNameX'='Value2'}
$subobject += [pscustomobject]@{'FieldName'='Value3';'FieldNameX'='Value4'}

# Create hashtable
$arr = @{}
$arr.Object1 = $subobject
$arr.NameX = 'ValueX'

Write-Host 'source array:'
$arr

# create pscustomobject
$myobject = [pscustomobject]$arr

Write-Host 'myobject before output with select-object -expandproperty:'
$myobject

Write-Host 'myobject output with select-object -expandproperty:'
$myobject | Select-Object * -ExpandProperty Object1

Write-Host 'myobject after output with select-object -expandproperty:'
$myobject

--- After running the script $myobject is changed:

NameX   : ValueX
Object1 : {@{FieldName=Value1; FieldNameX=Value2; NameX=ValueX; Object1=System.Object[]}, @{FieldName=Value3; FieldNameX=Value4; NameX=ValueX; Object1=System.Object[]}}

--- Expected content of myobject:

NameX   : ValueX
Object1 : {@{FieldName=Value1; FieldNameX=Value2}, @{FieldName=Value3; FieldNameX=Value4}}

Thank you!

Have a great day!

-Bruno.


Viewing all articles
Browse latest Browse all 10624

Trending Articles