Skip to content

Instantly share code, notes, and snippets.

@hotpocket
Last active September 17, 2025 18:20
Show Gist options
  • Save hotpocket/5b41da5b596f580f8ab8cc280bc09c4e to your computer and use it in GitHub Desktop.
Save hotpocket/5b41da5b596f580f8ab8cc280bc09c4e to your computer and use it in GitHub Desktop.
find orphaned windows users
# 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
}
}
@hotpocket
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment