PoshBytes: To Delete or To Empty Items? PowerShell Has Opinions

PoshBytes: To Delete or To Empty Items? PowerShell Has Opinions

Remove-ItemProperty and Clear-ItemProperty both change properties, but they do very different things. In this PoshBytes, we’ll look at when to delete the property and when to simply empty it out.

This post is a companion for the video embedded below. Scroll down to see the code from the video.

https://youtube.com/shorts/N51aKG6GR6w

Today we’re looking at the difference between the Remove-ItemProperty and Clear-ItemProperty cmdlets.

They sound similar, but the functionality is different. And knowing the difference can save you some headaches.

We’ll start with two registry keys, Winner and Score.

Clear-ItemProperty empties the value

Clear-ItemProperty does **not** delete the property.

It keeps it, but empties the value.

Think of it as an empty peanut butter jar your kids put back in the pantry.

Get-ItemProperty -Path $Path | 
  Select-Object Winner, Score

# Clear the value but keep the property
Clear-ItemProperty -Path $Path -Name 'Winner'

# Winner should still exist, but its value is blank
Get-ItemProperty -Path $Path | Select-Object Winner, Score

# Prove the property name still exists by listing properties
(Get-Item -Path $Path).Property

We see here that Winner still exists, it is just empty.

Remove-ItemProperty deletes the property

Remove-ItemProperty removes the property completely.

Not empty. Not blank. Gone.

# Remove the property entirely
Remove-ItemProperty -Path $Path -Name 'Score'

# Score should be missing now
Get-ItemProperty -Path $Path | Select-Object Winner, Score

# List remaining property names to confirm Score is gone
(Get-Item -Path $Path).Property

Now unlike the Clear, after the Remove the entire property is gone.

Like that peanut butter jar *should* have been. \

So I don’t get my hopes up on having a nice sandwich only to find an empty container…well anyways…yeah

Beyond the Registry

Technically, these commands are provider-based. \

Practically, they’re registry commands. \

Most other providers don’t support item properties, so you won’t really use them outside the registry.

# Set an environment variable
New-Item -Path Env:\ -Name 'DemoVar' -Value 'Hello'

# Will error on provider
Remove-ItemProperty -Path Env:\ -Name 'DemoVar'

If you try this outside the registry, PowerShell will politely inform you that this is not that kind of relationship.

Wrap Up

• Clear-ItemProperty clears the value but keeps the property.
• Remove-ItemProperty deletes the property completely.
• Use Clear when something should remain defined but empty.
• Use Remove when the setting should no longer exist.
• Always test registry changes carefully, because the registry remembers grudges.
• PoshBytes: Delete It or Empty It? Remove vs Clear ItemProperty
• PoshBytes: Registry Cage Match Clear-ItemProperty vs Remove-ItemProperty
• PoshBytes: Missing vs Null The Registry Difference That Bites

Leave a Reply

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