Remove a Member from a PowerShell Object?

前端 未结 4 606
春和景丽
春和景丽 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:08

    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.

提交回复
热议问题