Skip to content

Instantly share code, notes, and snippets.

@dpo007
Last active January 6, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpo007/f64bab239f67a93bb41f87139f32a49a to your computer and use it in GitHub Desktop.
Save dpo007/f64bab239f67a93bb41f87139f32a49a to your computer and use it in GitHub Desktop.
PowerShell function :: Delete matching user profile(s) from a Windows machine
# Warning: This cleans all profiles STARTING WITH THE USERNAME GIVEN. So "jsmith" would remove "jsmith", "jsmith.domain1.local", "jsmithly" etc.
function KillUserProfile {
Param (
[Parameter(Mandatory=$True)]
[string]$UserName
)
$profiles = Get-CimInstance win32_userprofile
foreach ($profile in $profiles) {
$localPathSplit = $profile.LocalPath.Split("\")
$profileFolderName = $localPathSplit[$localPathSplit.length-1]
if ($profileFolderName -like "$UserName*") {
Write-Host $profile
Remove-CimInstance $profile
Remove-Item $profile.LocalPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment