PSCustomObject and Select-Object Expandproperty - by bholliger

Status : 


Sign in
to vote
ID 770131 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 11/5/2012 2:04:46 PM
Access Restriction Public


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

# create pscustomobject
$myobject = [pscustomobject]$arr

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

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

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

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

Sign in to post a comment.