Recreating roaming profiles

This morning I was given the task of recreating a users roaming profile in a Citrix environment. This particular customer has 12 Citrix servers. I didn’t really feel like logging into each of them to remove the local copy of the users profile. So I wrote this powershell script to do it all for me.

The first line of this script pulls a list of servers form a .txt file. ServerList.txt is simply a list of server names, one per line.

Next I set a couple of variables to $Null to avoid accidentally removing the wrong profile.
Then we prompt for the username of the profile to be removed and confirm that the use is logged out.
The output of Get-WmiObject -Class Win32_UserProfile is much easier to filter based on SID, so the next line finds the SID of the user in questions.

Then the fun begins. First I rename the server side copy of the profile to append .old to the folder name. Always a good idea to make sure this is safe before continuing on. Honestly, this bit needs a bit of work. I’d like to add a bit more checking in here to make sure the server side profile has been renamed before continuing. For now I’m just checking it manually. I will update the script later to automate this check.

Finally I’m hitting up each of the servers listed in ServerList.txt and querying for profiles with matching SIDs. If the profile exists, remove it. Then just to make sure it’s gone, remove the local path if it exists.

$Servers = Get-Content C:tempServerList.txt

$RoamingPath = $NULL
$RoamingPathv1 = $NULL

$name = Read-Host -Prompt 'Username'
Write-Host "Please make sure the user is logged out of ALL Citrix servers"
[void](Read-Host 'Press Enter to continue')

$SID = (New-Object System.Security.Principal.NTAccount($name)).Translate([System.Security.Principal.SecurityIdentifier]).value

Try {
    Rename-Item -Path \serverctx_profiles$$Name.V2 -NewName \serverctx_profiles$$Name.V2.OLD -ErrorAction SilentlyContinue
    Rename-Item -Path \serverctx_profiles$$Name -NewName \serverctx_profiles$$Name.OLD -ErrorAction SilentlyContinue
Catch {
    Write-host "Could not rename profile"

ForEach ($Server in $Servers){

Write-Host "********** $Server ************" 
$LocalProfile = Get-WmiObject -Class Win32_UserProfile -ComputerName $Server -Filter "SID = '$SID'"
$LocalPath = $LocalProfile.localpath

$LocalProfile | Remove-WmiObject -ErrorAction SilentlyContinue

    Remove-Item -Path $LocalPath -Recurse -ErrorAction SilentlyContinue
    Write-host "Profile Path not there"


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s