Powershell Remove profile script - error checking does not work

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" } } 
+5
source share
2 answers

PowerShell has several automatic variables that you should avoid reusing.

$Profile is one of them; it contains the paths to the profile scripts applicable to the current session.

Use any other variable name (i.e. $userprofile ) and everything will be fine:

 foreach ($userprofile in $profiles) { $objSID = New-Object System.Security.Principal.SecurityIdentifier($userprofile.sid) $objuser = $objsid.Translate([System.Security.Principal.NTAccount]) $profilename = $objuser.value.split("\")[1] if($profilename -eq $UserName) { $profilefound = $true try { $userprofile.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" } } } 
+1
source

I managed to get it working by changing "$ profilefound = $ false" and making it a global variable. Also, the reason she was displaying a few lines of β€œprofile not found when I changed it to the else statement is because of where it was placed. He checked every profile on the server. When he touched every profile on the computer, it displayed "profile not found".

Here is a working script.

 $UserName=Read-host "Please Enter Username: " $ComputerName= @("computer1","computer2") $profilefound = "false" 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($userprofile in $profiles){ $objSID = New-Object System.Security.Principal.SecurityIdentifier($userprofile.sid) $objuser = $objsid.Translate([System.Security.Principal.NTAccount]) $profilename = $objuser.value.split("\")[1] if($profilename -eq $UserName) { $profilefound = "true" try { $userprofile.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" } } } } else { write-verbose "$Computer Not reachable" } if ($profilefound -eq "false") { Write-Host -ForegroundColor Cyan "No profiles found on $Computer with Name $UserName" } } 
0
source

All Articles