windowspowershellregistry

What is the equivalent powershell to this reg add with null value?


I'm trying to disable the annoying context menu in windows explorer using powershell.

The solution is supposedly using the following:

reg add "HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32" /f /ve

This works great, but I cannot get this to work using powershell's own registry functions. I've tried a variety of ways.

$LP2 = "HKLM:\SOFTWARE\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32\"
Set-ItemProperty -Path $LP2 -Type String -Value "" -Force | Out-Null    
#  FAILS

Set-ItemProperty -Path $LP2 -Type String -Value ([byte[]]::new(0)) -Force | Out-Null  
#  FAILS

# And finally
$LP2 = "HKLM:\SOFTWARE\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\"
Set-ItemProperty -Path $LP2 -Name "InprocServer32" -Type String -Value ([byte[]]::new(0)) -Force | Out-Null

# Results in the permission error:
# Set-ItemProperty: Requested registry access is not allowed.

What's going on, and what is the equivalent to the reg add above?


UPDATE
I tried with the suggested solution from admin shell and now get some permission error.

$LP2 = "HKLM:\SOFTWARE\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32"
Set-ItemProperty -Path $LP2 -Name '(default)' -Type String -Value '' -Force | Out-Null

# Set-ItemProperty: Requested registry access is not allowed.


Solution

  • Instead of trying to set a registry value to $null or an empty string using Set-ItemProperty, why not use Clear-ItemProperty instead?

    $LP2 = 'HKLM:\SOFTWARE\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32'
    Clear-ItemProperty -Path $LP2 -Name '(Default)'
    

    Of course, since you are modifying a registry value in the HKEY_LOCAL_MACHINE machine hive (HKLM:), you need admin permissions to do so.