Remove a Member from a PowerShell Object?

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

    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
    

提交回复
热议问题