Making sure your Citrix Desktops are utilized with Powershell v2
3 min read
Archive notice (October 2026): This post is from 2017 and is kept for reference only. It targets XenDesktop 7.x, now Citrix Virtual Apps and Desktops, and sends mail with Send-MailMessage and Basic auth against Office 365, which Microsoft marks obsolete and Exchange Online is retiring for SMTP AUTH. The script also puts desktops in maintenance mode on the same run that warns users they have 3 days, and removing a machine from the site does not delete the VM. Don’t run it as is. If you adapt the idea, filter on private desktops, stage the warning, maintenance mode and removal over separate days, and send mail through Microsoft Graph.
In my post from April on Making sure your Citrix Desktops are utilized in 35 lines of PoSH I mentioned I would be looking to expand the powershell script and I’ve added just a few lines of code to do that expantion.
The v1 version of the powershell script was able to grab all the desktops that were inactive for x amount of days (30 in my example) and emails the end user.
With this v1.1 release it will grab all the desktops that haven’t been used within x amount of days, notify the user and set the desktop to maintenance mode effectively making sure that the desktop can’t be used anymore. There’s an “if” statement added which will remove the desktop from the machine catalog if the desktop is older than 30 days and already in maintenance mode.
<# .SYNOPSIS Gets all desktops that haven't been used within x amount of days (30 days is the default) and emails the end user. .DESCRIPTION Gets all desktops that haven't been used within x amount of days (30 days is the default) and emails the end user. .EXAMPLE PS C:\PSScript > .\desktop_ultil_v1.ps1
.INPUTS
None. You cannot pipe objects to this script.
.OUTPUTS
No objects are output from this script.
This script creates an email to the end user.
.NOTES
NAME: desktop_ultil_v1.1.ps1
VERSION: 1.1
AUTHOR: Kees Baggerman
LASTEDIT: Aug 2017
#>
# Setting the variables for this script
$DDC = ""
$cred = get-credential
$adminmail = ""
# Making sure the appropriate cmdlets are loaded, Microsoft RSAT and Citrix PoSH Cmdlets need to be installed
Import-Module ActiveDirectory
Add-PSSnapin Citrix*
# Set current date minus 30 days
$logonDate = (get-date).AddDays(-30)
# Get all pre assigned desktops where the last login date is 30 days or longer
$Desktops = Get-BrokerDesktop -maxrecordcount 5000 -adminaddress $DDC | Select-Object MachineName, HostedMachineName, AssociatedUserNames,LastConnectionTime | where-object {$_.LastConnectionTime -le $logonDate}
# Loop through the results, fetch the corresponding email address of the user account and send out an email with a BCC to an additional email address
foreach($Desktop in $Desktops){
$user = Get-ADUser $Desktop.AssociatedUserNames.Split("\")[1] -Properties *
$ComputerName = $Desktop.HostedMachineName
$MachineName = $Desktop.MachineName
$body = “Your desktop ($ComputerName) will be deleted within 3 days”
Send-MailMessage -To $user.EmailAddress -bcc $adminmail -from xd-admin@ltd.com -Subject 'Your Virtual Desktop will be deleted soon' -Body $body -BodyAsHtml -smtpserver smtp.office365.com -usessl -Credential $cred -Port 587
if(Get-BrokerMachine $MachineName | where-object {$_.InMaintenanceMode -eq "true"}) {
remove-brokermachine -InputObject $MachineName -Force
} else {
Set-BrokerMachineMaintenanceMode -InputObject $MachineName $true }
}
As said before I had to use the Office 365 SMTP servers hence why you’d see the additional parameters for SSL, Creds and ports. This might be different for your own environment, also you need to make sure that the account you’re using to send the email has send-as rights for the alias you’re using for the -from parameter. This piece of script fetches all assigned desktops in a XD site, determines the last logon time and if it’s longer than 30 days it will collect the user assigned to the desktop all while utilizing posh cmdlets. Based on the user AD will be queried and the email address will be fetched to send out an email. So far, so good.. Result!

Kees Baggerman
Latest posts by Kees Baggerman (see all)
- Cloud Desktop Density: Where the Maths Breaks Down - November 5, 2026
- Egress Is the Second Lock-In: Modeling the Real Cost of Leaving Your Cloud Provider - November 4, 2026
- Stop Renting Their VM Layer: The Case for Hybrid Multicloud Infrastructure - October 28, 2026
- The Cloud Desktop CPU Lottery, AMD Edition: Which Zen Are You Actually Running On? - October 23, 2026
- Citrix MCSIO on Nutanix AHV: A Solution to a Problem That Doesn’t Exist - June 10, 2026


4 Comments