Last active
September 17, 2025 18:20
-
-
Save hotpocket/5b41da5b596f580f8ab8cc280bc09c4e to your computer and use it in GitHub Desktop.
find orphaned windows users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Notes. -- Some useful windows commands I used recently | |
| # shutdown no hibernate: `shutdown /s /t 0` | |
| # turn off hibernate: `powercfg /h off` | |
| # User profile info: `Get-CimInstance Win32_UserProfile | Select-Object LocalPath, SID, Loaded, Special` | |
| # Allow powershell script execution: `Set-ExecutionPolicy -Scope Process RemoteSigned` | |
| # Get all local users (to match SIDs) | |
| $localUsers = Get-LocalUser | Select-Object Name, SID | |
| Write-Host "`n=== Orphaned Profile Cleanup ===`n" | |
| # Enumerate all user profiles | |
| Get-CimInstance Win32_UserProfile | ForEach-Object { | |
| $path = $_.LocalPath | |
| $sid = $_.SID | |
| $special = $_.Special | |
| $loaded = $_.Loaded | |
| # Skip system, special, or currently loaded | |
| if ($special -or $loaded) { | |
| Write-Host "$path --> SKIPPED (special or in use)" -ForegroundColor Cyan | |
| return | |
| } | |
| # Check if there's a matching local user account | |
| $user = $localUsers | Where-Object { $_.SID -eq $sid } | |
| if (-not $user) { | |
| # Orphaned profile | |
| Write-Host "`nORPHANED profile found:`n Path: $path`n SID: $sid" -ForegroundColor Yellow | |
| $choice = Read-Host "Delete this profile? (Y/N)" | |
| if ($choice -match '^(Y|y)$') { | |
| try { | |
| $_ | Remove-CimInstance | |
| Write-Host "Deleted: $path" -ForegroundColor Green | |
| } | |
| catch { | |
| Write-Host "Error deleting $path : $_" -ForegroundColor Red | |
| } | |
| } | |
| else { | |
| Write-Host "Skipped: $path" -ForegroundColor DarkGray | |
| } | |
| } | |
| else { | |
| # Active profile | |
| Write-Host "$path --> ACTIVE (belongs to $($user.Name))" -ForegroundColor Green | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cleaning up accounts on a donated surface that is being dual booted. want the windows partition to be as small as possible. currently 30% of the drive space is orphaned profiles.