Wenn es darum geht, automatisiert bestimmte Registry Keys aus allen Profilen (HKEY_USERS) zu löschen, kann das natürlich auch mit Powershell erledigt werden.
Im folgenden Beispiel wird der Key „Software\clearbyte“ mit all den darunterliegenden Keys komplett aus jedem Profil gelöscht. Das .Default Profil und all welche länger als 8 Zeichen sind werden davon jedoch ausgeschlossen.
Ungünstigerweise ist der HKEY_USERS Hive nicht standardmässig als Drive in Powershell verfügbar (zumindest in der Version 2.0 auf meiner Maschine). Aus diesem Grund ermittle ich zuerst die aktuelle Position.
1 # Get current location to return to at the end of the script 2 $CurLoc = Get-Location
Dann muss HKEY_USERS Bereich gemountet werden.
1 # check if HKU branch is already mounted as a PSDrive. If so, remove it first 2 $HKU = Get-PSDrive HKU -ea silentlycontinue 3 #check HKU branch mount status 4 if (!$HKU ) { 5 # recreate a HKU as a PSDrive and navigate to it 6 New-PSDrive -Name HKU -PsProvider Registry HKEY_USERS | out-null 7 Set-Location HKU: 8 }
Danach kann darauf wie auf jedes andere gemountete Laufwerk zugegriffen und die Userprofile ausgelesen werden.
1 # select all desired user profiles, exlude *_classes & .DEFAULT 2 $regProfiles = Get-ChildItem -Path HKU: | ? { ($_.PSChildName.Length -le 8) -and ($_.PSChildName -notlike "*.DEFAULT") }
In einer For each Schleife werden dann die gefundenen Profile nach dem zu löschenden Registry Key durchforstet. Ist dieser vorhanden, wird er gelöscht.
1 # loop through all selected profiles & delete registry 2 ForEach ($profile in $regProfiles ) { 3 If(Test-Path -Path $profile\$strKey){ 4 Remove-Item -Path $profile\$strKey -recurse 5 } 6 }
Um statt eines ganzen Keys nur einzelne Werte zu löschen muss der Eintrag
1 Remove-Item -Path $profile\$strKey -recurse
durch den hier ersetzt werden.
1 Remove-ItemProperty -path "$profile\$strKey" -name "MyTestValue"
Am Schluss wird die ursprüngliche Position wieder hergestellt und der HKEY_USERS Bereich wird wieder abgehängt.
Der komplette Code:
1 # ============================================================================================== 2 # Author : clearByte 3 # Date : 18.09.2015 4 # Purpose : go through all user profiles in HKEY_USERS and delete desired key(s) 5 # ============================================================================================== 6 #GLOBALS ======================================================================================= 7 $strKey = "Software\clearbyte" 8 9 # Get current location to return to at the end of the script 10 $CurLoc = Get-Location 11 12 # check if HKU branch is already mounted as a PSDrive. If so, remove it first 13 $HKU = Get-PSDrive HKU -ea silentlycontinue 14 15 #END GLOBALS =================================================================================== 16 17 #MAIN ========================================================================================== 18 19 #check HKU branch mount status 20 if (!$HKU ) { 21 # recreate a HKU as a PSDrive and navigate to it 22 New-PSDrive -Name HKU -PsProvider Registry HKEY_USERS | out-null 23 Set-Location HKU: 24 } 25 26 # select all desired user profiles, exlude *_classes & .DEFAULT 27 $regProfiles = Get-ChildItem -Path HKU: | ? { ($_.PSChildName.Length -gt 8) -and ($_.PSChildName -notlike "*.DEFAULT") } 28 29 # loop through all selected profiles & delete registry 30 ForEach ($profile in $regProfiles ) { 31 If(Test-Path -Path $profile\$strKey){ 32 Remove-Item -Path $profile\$strKey -recurse 33 } 34 } 35 36 # return to initial location at the end of the execution 37 Set-Location $CurLoc 38 Remove-PSDrive -Name HKU 39 40 #END MAIN ======================================================================================