Locate Registry Key and delete value

738 Views Asked by At

I am trying to delete a binary value within my registry with this code the code prompts an error stating the value at DefaultConnectionSettings does exist but it's able to find the SID path, but not the exact DefaultConnectionSettings Value. I'm running this script on a test machine that has the DefaultConnectionSettings. RegEdit.exe screenshot

Any input would be helpful Thanks,

if (!(Test-Path 'HKU:\')) {
  New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
}

($path = Get-ChildItem -Path 'HKU:\*\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections' -ErrorAction SilentlyContinue) | 
  ForEach-Object { Remove-ItemProperty -Path $path -name "DefaultConnectionSettings" -force } 
1

There are 1 best solutions below

2
mklement0 On

The registry value you're trying to delete is a value of the registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections registry keys themselves.

  • Note that you can target the HKEY_USERS hive simply by prepending the provider prefix registry:: to the native registry path - no need to map a new drive with New-PSDrive first.

By contrast, Get-ChildItem looks for subkeys of the targeted keys.

  • Note that registry values are considered properties of registry keys, not child items (the way that files are in the file-system).

Thus, the immediate fix is to switch from Get-ChildItem to Get-Item, which returns objects representing the target keys themselves.

However, you can do it all with a single Remove-ItemProperty call (as with your own attempt, running from an elevated session is assumed):

$path = 'registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
Remove-ItemProperty -WhatIf -Force -Path $path -Name DefaultConnectionSettings -ErrorAction SilentlyContinue

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.