I have this script deletion profile that requests a username and deletes it from each of the listed computers. The deletion elements and "user logged in" work, but the part that says: "There are no profiles found on the computer named $ UserName" does not. I ran my script on two computers and it successfully deleted my profile on both. I recreated my profile (logged in) and remained included in one and not the other. I run it again and it gives me the message "user logged in". For another computer, he simply deleted the profile without displaying the message "no profile found". It just skips it and doesn't display anything. I changed the "if" to "else", but when I do this, it displays several lines "without profiles", including the computer on which it previously deleted the profile.
Here is the link that most of the script comes from. http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556 . Looking through the comments, someone else had no problems with this part.
I don't have much knowledge in PowerShell, and it has just been compiled from other scripts that I found based on our needs. Our environment is Windows 7 and Server 2008 R2. Any help is greatly appreciated.
$UserName=Read-host "Please Enter Username: " $ComputerName= @("computer1","computer2") foreach($Computer in $ComputerName) { Write-Verbose "Working on $Computer" if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) { $Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $Computer -ea 0 foreach ($profile in $profiles) { $objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.sid) $objuser = $objsid.Translate([System.Security.Principal.NTAccount]) $profilename = $objuser.value.split("\")[1] if($profilename -eq $UserName) { $profilefound = $true try { $profile.delete() Write-Host -ForegroundColor Green "$UserName profile deleted successfully on $Computer" } catch { Write-Host -ForegroundColor Yellow "Failed to delete the profile, $UserName logged on to $Computer" } } } if(!$profilefound) { Write-Host -ForegroundColor Cyan "No profiles found on $Computer with Name $UserName" } } else { write-verbose "$Computer Not reachable" } }
source share