Remove a Member from a PowerShell Object?

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

    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.

提交回复
热议问题