PowerShell: How to Remove a Property from an Object


Often you may want to remove a specific property from an object in PowerShell.

You can use the following syntax to do so:

$my_object.PSObject.Properties.Remove('my_property')

This particular example removes the property named my_property from the object named $my_object.

The following example shows how to use this syntax in practice.

Example: How to Remove Property from an Object in PowerShell

Suppose that we create the following object in PowerShell named $my_object:

$my_object = New-Object -TypeName PSObject -Property @{ Team='Mavs'; Points=22; Assists=8; Rebounds=10 }

The following screenshot shows how to use this syntax in practice:

Suppose that we would like to remove the property named Points from this object.

We can use the following syntax to do so:

$my_object.PSObject.Properties.Remove('points')

The following screenshot shows how to use this syntax in practice:

PowerShell remove property from object

Notice that when we view $my_object again we can see that the Points property is no longer shown.

We can also use the -contains command to verify that our object no longer contains this property:

$my_object.PSobject.Properties.Name -contains 'Points' 

The following screenshot shows how to use this syntax in practice:

This returns False, which confirms that the object named $my_object no longer contains a property named Points.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Check if Object has Specific Property
PowerShell: How to List All Object Properties
PowerShell: How to Use Group-Object with Multiple Properties
PowerShell: How to Use Sort-Object with Multiple Properties

Leave a Reply