Search code examples
powershellpowershell-3.0

Remove a Member from a PowerShell Object?


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


Solution

  • 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')