Local Machine Profile and Registry Remove

HAHA.. this one seems to very interesting to me. One of the engineer asked me write this for him, he has to remove unwanted profiles from servers again talking about 500+ servers

I wrote the below script giving him the ability to put in account that needs to be left alone and anything else can go to bin along with the registry. which got me a lunch 😛

$machinename = Read-Host "Please Type the host Name "

$existingusers = Get-ChildItem "\\$machinename\c$\users" | select name

foreach($username in $existingusers){
$checkforaccount =$username.Name.Contains("account name that needs to remain")

            if($checkforaccount -eq "True"){
       
            }else{
                $name = $username.Name
                Get-WmiObject -class Win32_UserProfile -ComputerName "$machinename" | ? {$_.LocalPath -eq "C:\Users\$name"} | Remove-WmiObject
            }
    
}

Published by iamfazul

Author of the site

Leave a comment