70

I need to remove a member (specifically, a NoteProperty) from an object. How do I accomplish this?

4 Answers 4

96

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')
Sign up to request clarification or add additional context in comments.

9 Comments

Ah! I was tooling around with the Remove method and couldn't get it to work, but it makes sense that you'd have to apply it specifically to the class with the properties in it. Thanks to both of you.
I was messing around with Remove last night and from what I can tell it doesn't always work. Try Get-Process -id $pid | % { $_.psobject.properties.remove('__NounName'); $_} | % __NounName. It is not surprising it would work with a psobject or pscustomobject even. OTOH I was able to get it with DisplayHint on Get-Date.
That's a good point @KeithHill. I too discovered later some objects are immutable, or at least cannot be altered this way. You can witness the same symptoms if you try to manually change one of their properties, e.g. $_.__NounName = "some new value"
@Tohuw: I think what it comes down to is that you can only remove NoteProperty members this way. While they are typically found on custom PS objects, PS also occasionally adds NoteProperty properties to .NET types (whose properties are of type Property), such as DisplayHint on [System.DateTime], which explains Keith's experience.
The down-side with using select is it forces you to input every single property you want instead of excluding one property. This can be a huge burden on objects with lots of properties
|
37

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.

1 Comment

The part -Property * is really necessary or it doesn't work. Thanks for the tip Andy!
12

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.

1 Comment

While I see what you're going for there, that involves lots of mutation I don't want to perform on an object when the above methods work fine and don't require that.
3

If can depend on the type of object or collection you want to remove from. Commonly its a Collection (array) of objects like you might get from 'import-csv' which you can do it pretty easily.

$MyDataCollection = Import-CSV c:\datafiles\ADComputersData.csv
$MyDataCollection
Windows Server : lax2012sql01
IP             : 10.101.77.69
Site           : LAX
OS             : 2012 R2
Notes           : V

Windows Server : sfo2016iis01
IP             : 10.102.203.99
Site           : SFO
OS             : 2012 R2
Notes           : X

The to remove a property from each of these:

$MyDataCollection | ForEach { $_.PSObject.Properties.Remove('Notes') }

Windows Server : lax2012sql01
IP             : 10.101.77.69
Site           : LAX
OS             : 2012 R2

Windows Server : sfo2016iis01
IP             : 10.102.203.99
Site           : SFO
OS             : 2012 R2

1 Comment

No need for the loop: $MyDataCollection.PSObject.Properties.Remove('Notes') also does the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.