How to Remove Citrix User Profiles in Bulk


If you have one or more Citrix XenApp environments in your organization, you probably have a lot of Windows user profiles to manage. Citrix servers are great for virtual desktops but tend to multiply a user profile management headache. When user accounts are removed from AD, what happens to those profiles on one or more of your Citrix servers? Absolutely nothing.

In this article, you’re going to learn how to write a PowerShell script that searches a Citrix XenApp environment to remove one or more user profiles.

Script Summary

The script demonstrated in this article will:

          Connect to a Citrix farm’s Zone Data Collector (ZDC)

          Find all servers in a farm via the ZDC

          Find all user profiles on each server in the farm

          Remove all user profiles matching a profile name

Finding all Servers in a Farm

The first task in finding any problematic user profiles is to connect to the farm’s ZDC. Unfortunately, there’s no good way to programmatically do that. You must first manually specify the hostname or the IP address of a farm’s ZDC.

Open up the code editor window of your choice and first define a variable representing the ZDC server to connect to.

$ZdcServer = '<zdc_server_name_here>'

Next, you’ll need to connect to the ZDC server using PowerShell Remoting. In the following code snippet, you’ll see that using the Invoke-Command cmdlet, PowerShell is loading all Citrix snapins on the ZDC server and then running Get-XaServer to return all servers in a particular farm.

$Block = {
Add-PSSnapin Citrix*;
Get-Xaserver | Where-Object { $_.ElectionPreference -eq 'WorkerMode' } | Select-Object -ExpandProperty ServerName
}
$XaServers = Invoke-Command -ComputerName $ZdcServer -ScriptBlock $Block | Sort-Object

Building Loops to Search All Servers and Profiles

Let’s say you’re looking for a few user profiles called adam and david. Define both of these in an array.

$UserName = 'adam','david'

Next, create two foreach loops. You need one loop to search for all usernames in the $UserName array. Since the script needs to check all servers found, inside of the username loop, place a foreach loop to query each server.

foreach ($User in $Username) {
foreach ($Server in $XaServers) {

}
}

Inside of the $XaServers loop is where all of the action happens. First, the script finds all of the user profiles that exist on each server.

$getProfParams = @{
'ClassName' = 'Win32_UserProfile'
'ComputerName' = $Server
}
$UserProfiles = Get-CimInstance @getProfParams | Where-Object { $_.LocalPath.Split('\')[-1] -match "$User$" }

Once the scripts finds all of the user profiles on the particular server instance, it’s now time to remove all of the profiles found. Below you can see a code snippet that runs Remove-CimInstance for each of the profiles found above with Get-CimInstance.

$UserProfiles | foreach {
try {
$_ | Remove-CimInstance
} catch [exception] {
Write-Warning -Message $_.Exception.Message
}
}

At this point, the PowerShell script has done its job. You can find the entire script below.

$ZdcServer = '<zdc_server_name_here>'
$UserName = 'adam','david'

$Block = {
Add-PSSnapin Citrix*;
Get-Xaserver | Where-Object { $_.ElectionPreference -eq 'WorkerMode' } | Select-Object -ExpandProperty ServerName
}
$XaServers = Invoke-Command -ComputerName $ZdcServer -ScriptBlock $Block | Sort-Object

foreach ($User in $Username) {
foreach ($Server in $XaServers) {
$getProfParams = @{
'ClassName' = 'Win32_UserProfile'
'ComputerName' = $Server
}
$UserProfiles = Get-CimInstance @getProfParams | Where-Object { $_.LocalPath.Split('\')[-1] -match "$User$" }
$UserProfiles | foreach {
try {
$_ | Remove-CimInstance
} catch [exception] {
Write-Warning -Message $_.Exception.Message
}
}
}

Conclusion

By piecing together a few tasks, you can build a PowerShell script that removes any number of user profiles on Citrix Xenapp servers for you automatically. There’s no need to manually search down problematic, stale or user profiles that are taking up too much storage.

With the script covered in this article, you can now rest assure you no longer have to worry about finding and cleaning up Windows user profiles on your Citrix XenApp servers.

Tags

Get Started with WhatsUp Gold

Subscribe to our mailing list

Get our latest blog posts delivered in a weekly email.

<p class='-pt2 -m0'>Thanks for subscribing!</p> Loading animation

Comments
Comments are disabled in preview mode.