PoshCode Logo PowerShell Code Repository

Remove-LocalProfile by Brian 6 weeks ago (modification of post by Boe Prox view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6842"></script>download | new post

This is script will first ask for a computername and then will scan the Win32_UserProfile WMI class and present the user with all of the possible user profiles to remove. After the profile has been deleted, the user has a choice to continue to remove another profile or quit. This script will only work against Vista and above client OS’s and Window 2008 and above server OS’s, but can be ran from any OS that has PowerShell installed. Updated to include Windows 10 and Server 2016.

  1. <#  
  2. .SYNOPSIS  
  3.     Interactive menu that allows a user to connect to a local or remote computer and remove a local profile.
  4. .DESCRIPTION
  5.     Presents an interactive menu for user to first make a connection to a remote or local machine.  After making connection to the machine,  
  6.     the user is presented with all of the local profiles and then is asked to make a selection of which profile to delete. This is only valid
  7.     on Windows Vista OS and above for clients and Windows 2008 and above for server OS.    
  8. .NOTES  
  9.     Name: Remove-LocalProfile
  10.     Author: Boe Prox
  11.     DateCreated: 26JAN2011      
  12. .LINK  
  13.     http://boeprox.wordpress.com
  14.     http://msdn.microsoft.com/en-us/library/ee886409%28v=vs.85%29.aspx
  15. .EXAMPLE  
  16. Remove-LocalProfile
  17.  
  18. Description
  19. -----------
  20. Presents a text based menu for the user to interactively remove a local profile on local or remote machine.    
  21. #>  
  22.  
  23. #Prompt for a computer to connect to
  24. $computer = Read-Host "Please enter a computer name"
  25. #Test network connection before making connection
  26. If ($computer -ne $Env:Computername) {
  27.     If (!(Test-Connection -comp $computer -count 1 -quiet)) {
  28.         Write-Warning "$computer is not accessible, please try a different computer or verify it is powered on."
  29.         Break
  30.         }
  31.     }
  32. Try {    
  33.     #Verify that the OS Version is 6.0 and above, otherwise the script will fail
  34.     If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt ("0:D2" -f 6.0)) {
  35.         Write-Warning "The Operating System of the computer is not supported.`nClient: Vista and above`nServer: Windows 2008 and above."
  36.         Break
  37.         }
  38.     }
  39. Catch {
  40.     Write-Warning "$($error[0])"
  41.     Break
  42.     }    
  43. Do {    
  44. #Gather all of the user profiles on computer
  45. Try {
  46.     $users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter "LocalPath Like 'C:\\Users\\%'" -ea stop
  47.     }
  48. Catch {
  49.     Write-Warning "$($error[0]) "
  50.     Break
  51.     }    
  52. #Cache the number of users
  53. $num_users = $users.count
  54.  
  55. Write-Host -ForegroundColor Green "User profiles on $($computer):"
  56.  
  57.     #Begin iterating through all of the accounts to display
  58.     For ($i=0;$i -lt $num_users; $i++) {
  59.         Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\',''))"
  60.         }
  61.     Write-Host -ForegroundColor Green "q: Quit"
  62.     #Prompt for user to select a profile to remove from computer
  63.     Do {    
  64.         $account = Read-Host "Select a number to delete local profile or 'q' to quit"
  65.         #Find out if user selected to quit, otherwise answer is an integer
  66.         If ($account -NotLike "q*") {
  67.             $account = $account -as [int]
  68.             }
  69.         }        
  70.     #Ensure that the selection is a number and within the valid range
  71.     Until (($account -lt $num_users -AND $account -match "\d") -OR $account -Like "q*")
  72.     If ($account -Like "q*") {
  73.         Break
  74.         }
  75.     Write-Host -ForegroundColor Yellow "Deleting profile: $(($users[$account].localpath).replace('C:\Users\',''))"
  76.     #Remove the local profile
  77.     ($users[$account]).Delete()
  78.     Write-Host -ForegroundColor Green "Profile:  $(($users[$account].localpath).replace('C:\Users\','')) has been deleted"
  79.  
  80.     #Configure yes choice
  81.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Remove another profile."
  82.  
  83.     #Configure no choice
  84.     $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Quit profile removal"
  85.  
  86.     #Determine Values for Choice
  87.     $choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes,$no)
  88.  
  89.     #Determine Default Selection
  90.     [int]$default = 0
  91.  
  92.     #Present choice option to user
  93.     $userchoice = $host.ui.PromptforChoice("Information","Remove Another Profile?",$choice,$default)
  94.     }
  95. #If user selects No, then quit the script    
  96. Until ($userchoice -eq 1)

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me