Remove Property from Object in PowerShell

Remove property from Object in PowerShell

Using PSObject.Properties.Remove() Method

To delete a property from an object in PowerShell,

  • Use the New-Object cmdlet 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 a string parameter.

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 a string parameter, so you need to pass the name of the property you want to remove.

Using -ExcludeProperty Parameter

To delete the specified property from an object in PowerShell,

  • Use the New-Object cmdlet to create an object
  • Use the Select-Object cmdlet with the -ExcludeProperty parameter to remove specified property from the object.

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 representing all. That means the above command is used to select all the properties of the car_obj object, excluding the Type property 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:

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.

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-Object with -ExcludeProperty is suitable for removing a property from the collection of objects. For deleting a property from a single object PSObject.Properties.Remove() method might be more effective.

That’s all about how to remove property from Object in PowerShell,

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *