Last active
January 6, 2020 18:02
-
-
Save dpo007/f64bab239f67a93bb41f87139f32a49a to your computer and use it in GitHub Desktop.
PowerShell function :: Delete matching user profile(s) from a Windows machine
This file contains 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
# 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