Remove a Member from a PowerShell Object?
Select-Object
with ExcludeProperty
is good for removing a property from a collection of objects.
For removing a property from a single object this method might be more effective:
# new object with properties Test and Foo$obj = New-Object -TypeName PSObject -Property @{ Test = 1; Foo = 2 }# remove a property from PSObject.Properties$obj.PSObject.Properties.Remove('Foo')
I don't think you can remove from an existing object but you can create a filtered one.
$obj = New-Object -TypeName PsObject -Property @{ Test = 1}$obj | Add-Member -MemberType NoteProperty -Name Foo -Value Bar$new_obj = $obj | Select-Object -Property Test
Or
$obj | Select-Object -Property * -ExcludeProperty Foo
This will effectively achieve the same result.
I found the following helps if you're interested in removing just one or two properties from a large object. Convert your object into JSON then back to an object - all the properties are converted to type NoteProperty, at which point you can remove what you like.
$mycomplexobject = $mycomplexobject | ConvertTo-Json | ConvertFrom-Json $mycomplexobject.PSObject.Properties.Remove('myprop')
The conversion to JSON and back creates a PSCustomObject. You'll have the original object expressed and then you can remove as desired.