简体   繁体   中英

WMI Registry Editing Using Powershell

I'm trying to use powershell to empty out a registry key. When I run the following code, all seems fine and well with no errors, but upon checking the registry the key's contents still remain.

I want to also add a target computer to the script to remove the defunct key from target machines.

Any Idea what I'm doing wrong? Thanks guys.

$hklm = 2147483650
$key = "SOFTWARE\Microsoft\MSLicensing"
$wmi = [wmiclass]"root\default:stdRegProv"
$wmi.DeleteKey($hklm,$key)
$hklm = 2147483650
$rootKey = "SOFTWARE\Microsoft\MSLicensing"
$wmi = [wmiclass]"root\default:stdRegProv"
#$wmi.DeleteKey($hklm,$key)

[string[]]$subkeys = $wmi.EnumKey($hklm, $key).sNames

foreach( $subkey in $subkeys ) {

    $key = $rootKey + "\$subkey"

    $wmi.DeleteKey($hklm, $key)
}

$wmi.DeleteKey($hklm, $rootKey)

The reason your code was failing is that there are keys underneath the key you are trying to delete.

The above code should fix your issue.

cd hklm:\\ SOFTWARE \\ Microsoft \\ MSLicensing remove-item 2147483650 -whatif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM