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
= { |
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) { |
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 = @{ |
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 { |
At this point, the PowerShell script has done its job. You can find the entire script below.
$ZdcServer = '<zdc_server_name_here>' |
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.