Remove a Member from a PowerShell Object?

前端 未结 4 587
春和景丽
春和景丽 2021-02-03 17:54

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

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-03 18:01

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

提交回复
热议问题