User registry in allen aktiven HKEY_USERS Profilen löschen mit Powershell

Erstellt am 27. November 2015

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.

# Get current location to return to at the end of the script
$CurLoc = Get-Location

Dann muss HKEY_USERS Bereich gemountet werden.

# check if HKU branch is already mounted as a PSDrive. If so, remove it first
$HKU = Get-PSDrive HKU -ea silentlycontinue
#check HKU branch mount status
if (!$HKU ) {
 # recreate a HKU as a PSDrive and navigate to it
 New-PSDrive -Name HKU -PsProvider Registry HKEY_USERS | out-null
 Set-Location HKU:
}

Danach kann darauf wie auf jedes andere gemountete Laufwerk zugegriffen und die Userprofile ausgelesen werden.

# select all desired user profiles, exlude *_classes & .DEFAULT
$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.

# loop through all selected profiles & delete registry
ForEach ($profile in $regProfiles ) {
 If(Test-Path -Path $profile\$strKey){
 Remove-Item -Path $profile\$strKey -recurse
 }
}

Um statt eines ganzen Keys nur einzelne Werte zu löschen muss der Eintrag

Remove-Item -Path $profile\$strKey -recurse

durch den hier ersetzt werden.

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:

# ============================================================================================== 
# Author : clearByte 
# Date : 18.09.2015 
# Purpose : go through all user profiles in HKEY_USERS and delete desired key(s) 
# ============================================================================================== 
#GLOBALS ======================================================================================= 
$strKey = "Software\clearbyte" 

# Get current location to return to at the end of the script 
$CurLoc = Get-Location 

# check if HKU branch is already mounted as a PSDrive. If so, remove it first 
$HKU = Get-PSDrive HKU -ea silentlycontinue 

#END GLOBALS =================================================================================== 

#MAIN ========================================================================================== 

#check HKU branch mount status 
if (!$HKU ) { 
	# recreate a HKU as a PSDrive and navigate to it 
	New-PSDrive -Name HKU -PsProvider Registry HKEY_USERS | out-null 
	Set-Location HKU: 
} 

# select all desired user profiles, exlude *_classes & .DEFAULT 
$regProfiles = Get-ChildItem -Path HKU: | ? { ($_.PSChildName.Length -gt 8) -and ($_.PSChildName -notlike "*.DEFAULT") } 

# loop through all selected profiles & delete registry 
ForEach ($profile in $regProfiles ) { 
	If(Test-Path -Path $profile\$strKey){ 
	Remove-Item -Path $profile\$strKey -recurse 
	} 
} 

# return to initial location at the end of the execution 
Set-Location $CurLoc 
Remove-PSDrive -Name HKU 

#END MAIN ======================================================================================

Erstellt am 27. November 2015

Vielleicht auch interessant…

MSIX – Was heisst das für mein Team? (Teil 2)

MSIX – Was heisst das für mein Team? (Teil 2)

Herzlich willkommen zur MSIX-Blogserie, in der ich für unsere Leserinnen und Leser eine Übersicht über MSIX, bekannterweise das neu lancierte Installationsformat von Microsoft, bereitstelle. Der erste Teil der Serie ist hier nachzulesen, ich wünsche viel Vergnügen....

mehr lesen

This Area is Widget-Ready

You can place here any widget you want!

You can also display any layout saved in Divi Library.

Let’s try with contact form: