Using PSObject.Properties.Remove() Method
To delete a property from an object in PowerShell,
- Use the
New-Objectcmdlet to create an object. - Use the
PSObject.Properties.Remove()method to remove a specific property from an object by providing the property’s name as astringparameter.
|
1 2 3 4 5 6 7 8 9 10 |
$person_obj = New-Object -Typename PSCustomObject -Property @{ Name = 'Bob' Age = '25' Citizenship = 'U.S' } Write-Host "Before Removal: $person_obj" $person_obj.PSObject.Properties.Remove('Age') Write-Host "After Removal: $person_obj" |
|
1 2 3 4 |
Before Removal: @{Citizenship=U.S; Name=Bob; Age=25} After Removal: @{Citizenship=U.S; Name=Bob} |
In the above code, we have created an object called$person_obj having Name, Age and Citizenship as its properties. Next, we used the Remove() method to remove the Age property. You can observe $person_obj was only left with two properties, Citizenship and Name. In this command, we have used the PSObject.Properties.Remove() method to remove the property Age from the object $person_obj.
Note:
PSObject.Properties.Remove()method also takes the property’s name as astringparameter, so you need to pass the name of the property you want to remove.Further reading:
Using -ExcludeProperty Parameter
To delete the specified property from an object in PowerShell,
- Use the
New-Objectcmdlet to create an object - Use the
Select-Objectcmdlet with the-ExcludePropertyparameter to remove specified property from the object.
|
1 2 3 4 5 6 7 8 9 10 |
$car_obj = New-Object -Typename PSCustomObject -Property @{ Name = 'BMW' Color = 'White' Type = 'Manual' } Write-Host "Before Removal: $car_obj" $car_obj = $car_obj | Select-Object * -ExcludeProperty Type Write-Host "After Removal: $car_obj" |
|
1 2 3 4 |
Before Removal: @{Color=White; Name=BMW; Type=Manual} After Removal: @{Color=White; Name=BMW} |
In the above code, New-Object cmdlet was used to create a PSCustomObject called car_obj having three properties Color, Name and Type. Here, the -Property parameter was used to set the properties of the car object in the key-value pair format.
In this example, Select-Object cmdlet was used to select the specified property of car_obj object. Here, the -ExcludeProperty parameter was used to remove the property Type from the $car_obj.
In PowerShell, the -ExcludeProperty parameter takes the property’s name as a string parameter, so you must pass the name of the property you want to remove.
Note: In this code snippet
*is representingall. That means the above command is used to select all the properties of thecar_objobject, excluding theTypeproperty we wanted to remove.
You can also remove multiple properties by specifying the names of all properties you want to remove as the value for the -ExcludeProperty parameter. As shown below example:
|
1 2 3 4 5 6 7 8 |
$car_obj = New-Object -Typename PSCustomObject -Property @{ Name = 'BMW' Color = 'White' Type = 'Manual' } $car_obj | Select-Object * -ExcludeProperty Name, Color |
|
1 2 3 4 5 |
Type ---- Manual |
In this example, the properties Name and Color were removed from the object $car_obj using the -ExcludeProperty parameter.
Let’s see another example of using the -ExcludeProperty parameter to remove multiple properties from an object. It takes one or more property names as arguments and removes them from the object.
|
1 2 3 4 5 6 7 8 |
$my_obj = New-Object -Typename PSCustomObject -Property @{ 'property1' = 'value1' 'property2' = 'value2' 'property3' = 'value3' } $my_obj | Select-Object * -ExcludeProperty property1, property3 |
|
1 2 3 4 5 |
property2 ---------- value2 |
In this example, we have created an object $my_obj, which contained three properties property1, property2 and property3. After that, the -ExcludeProperty parameter was used to remove property1 and property3. And the object is now only left with property2 having value value2.
Note:
Select-Objectwith-ExcludePropertyis suitable for removing a property from the collection of objects. For deleting a property from a single objectPSObject.Properties.Remove()method might be more effective.
That’s all about how to remove property from Object in PowerShell,