Powershell delete profiles from registry

Hi,

Before we provision our desktopservers we need to delete all userprofiles from the registry except the following usernames: Administrator and CTX_Streaminig.

This is what i got now:

$REGPROF=REGISTRY::HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList 
$PROFLIST=GET-CHILDITEM $REGPROF 
FOREACH ( $ITEM in $PROFLIST )

{ 

$userid=(get-itemproperty registry::$item).ProfileImagePath

write-host @userid 
if ($userid -notlike @("*administrator*","*Ctx_StreamingSvc","*NetworkService*","*Localservice*","*systemprofile*")) { $userid; remove-item registry::$item } 

}

Why isn't it working, my administrator account is deleted and networkservice, localservice and systemprofiles also?

write-host @ userid shows:

C:\Windows\system32\config\systemprofile
C : \ W i n d o w s \ S e r v i c e P r o f i l e s \ L o c a l S e r v i c e

etc. There are spaces in between?

Please help me out here....

Thanks!



December 19th, 2014 8:16am

This does not delete the profile.  It deletes only the registry reference.  It willbe replaced the next time the system is started.  TOremove prfiles you will need ti use WMI.  WMI will skip critical profiles.

If you are trying to fix hung profiles you may need to do this differently.

Free Windows Admin Tool Kit Click here and download it now
December 19th, 2014 10:45am

Be careful:

Get-ChildItem HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList |
    ForEach-Object{
        $profilepath=$_.GetValue('ProfileImagePath')    
        if($profilepath -notmatch 'administrator|Ctx_StreamingSvc|NetworkService|Localservice|systemprofile'){
            Write-Host "Removing item: $profilepath" -ForegroundColor green
            Remove-Item $_.PSPath -Whatif
        }else{
            Write-Host "Skipping item:$profilepath" -Fore blue -Back white
	    }
    }

December 19th, 2014 11:03am

Thank you for your reply!

I will test this tomorrow and keep you updated!

Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2014 3:22pm

Exactly what it need to do! Thanks a lot!
December 23rd, 2014 2:30pm

This was an awesome help thanks! For deleting the profile folders I did something pretty simple without WMI commands and just combined it with your script.  The variable $Objectpath isn't really necessary since it's an unchanging short path, but it makes it easier for me to modify the script.
$objectpath = "C:\Users\"

Remove-Item -path "$objectpath*" -Exclude Administrator, Default, Public -Confirm

Get-Childitem -path "$objectpath*" | Out-Gridview -Title "REMAINING USER ACCOUNTS ON YOUR COMPUTER"
I do have a question, I can display the results for the folders as you can see, but how can I export the results from your script?
Free Windows Admin Tool Kit Click here and download it now
June 17th, 2015 5:13pm

This topic is closed and has been for some time. If you have a ne question please start you own topic and someone will try to help you.

If you proceed  to delete profiles the way you are you will absolutely crash some systems.  DO NOT DO IT your way.

June 17th, 2015 5:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics