Last active
December 11, 2017 18:42
-
-
Save sellynx/6b3501a984d298eb23ff to your computer and use it in GitHub Desktop.
useful to delete user profile on Windows Terminal Server (and Citrix)
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
<# | |
.SYNOPSIS | |
Remove user profiles from local or remote computer | |
.PARAMETER | |
infosrv [[-computers] <string[]>] [[-username] <string>] | |
.DESCRIPTION | |
the script accepts array of computers passed by pipe. | |
if no computer is passed, the script will work on local computer | |
username is a mandatory parameter | |
the script will remove the user profiles and the registry keys associated with the printers, if there is any | |
HKLM\software\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\USERNAME-SID | |
this scripts is useful on TS server. | |
.EXAMPLES | |
remove_userprofile.ps1 "server1","server2" | |
remove_userprofile.ps1 "server1","server2" -username UsernameToRemove | |
remove_userprofile | |
remove_userprofile -username UsernameToRemove | |
servers | remove_userprofile | |
servers | remove_userprofile -username UsernameToRemove | |
.NOTES | |
Author : Selene | |
#> | |
[cmdletbinding()] | |
param( | |
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[string[]]$ComputerName = $env:computername, | |
[parameter(mandatory=$true)] | |
[string]$UserName | |
) | |
Begin {} | |
Process { | |
foreach($Computer in $ComputerName) { | |
Write-Verbose "Working on $Computer" | |
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) { | |
$Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $Computer -ea 0 | |
foreach ($profile in $profiles) { | |
$present= $profile.SID | |
$ID = new-object System.Security.Principal.NTAccount($UserName) | |
$usersid= $ID.Translate( [System.Security.Principal.SecurityIdentifier] ).toString() | |
if($present -eq $usersid) { | |
$profilefound = $true | |
try { | |
$profile.delete() | |
Write-Host "$UserName profile deleted successfully on $Computer" | |
} catch { | |
Write-warning "Failed to delete the profile, $UserName on $Computer" | |
} | |
} | |
if (reg query "\\$computer\HKLM\software\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\$usersid" 2>null ){ | |
try { | |
reg delete "\\$computer\HKLM\software\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\$usersid" /f | |
Write-Host "Client Side Rendering Print Provider\$UserName deleted successfully on $Computer" | |
} catch { | |
Write-warning "Failed to delete the profile, Client Side Rendering Print Provider\$UserName on $Computer" | |
} | |
} | |
} | |
if(!$profilefound) { | |
write-Warning "No profiles found on $Computer with Name $UserName" | |
} | |
} else { | |
write-verbose "$Computer Not reachable" | |
} | |
} | |
} | |
end {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment