[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


PowerShell Commands to Verify the VDI Usage and Delete the Existing VDI from DDC Remotely

Published on
5,754 Points
3 Endorsements
Last Modified:
PowerShell is a very powerful scripting language that can help automate many aspects of maintaining and deploying a XenDesktop environment. To share some of my experiences creating PowerShell scripts, I’ve created this article that will give guidance and ideas for creating your own scripts for automating XenDesktop VDI usage and to delete the VDI if not used.

Why to Automate this?
First off, Why should you use Powershell rather than doing certain tasks manually? While automation might seem faster, time must be spent upfront developing the scripts. It is very easy to say it takes a few clicks to complete the process, when you have the automated script it just needs to you do a single click and the process is complete.

PowerShell Commands to Verify the VDI usage and Delete the Existing VDI from DDC Remotely :
The purpose of this article is to automate the process to verify the VDI usage and to delete the VDIs remotely using the PowerShell commands.

The below mentioned method does not require us to login to the server or even to the Desktop studio which helps us to save a lot time to delete the VDI.

This includes the following steps:
Step 1: First load the Citrix and VMware Snapins to run the commands using PowerShell. 
Note:The XenDesktop SDK comprises of a number of PowerShell snap-ins which are installed automatically by the XenDesktop installation wizard when you install either the controller or Desktop Studio components.
Step 2: Declare the common variables that are used in the script – like the Controller name, VDI Name and Desktop group name.
Step 3 : Verify the VDI usage.
Step 4: Inorder to delete the VDI we need to enable the maintenance mode of the VDI.
Step 5: Shutdown the VDI.
Step 6: Delete the VDI from the Desktop group.
Step 7: Delete the VDI from the Machine Catalog.

Step 1: PowerShel command to “Load the Citrix and VMWare Snapins”
Asnp Citrix.*
Add-PSSnapin VMware.VimAutomation.Core

Open in new window

Step 2: Declare the Common Variables:

$DDC = “DDC ServerName”
$VDI = “DomainName\VDI Name”
$DesktopGroup = “Desktop Group Name”

Open in new window

Step 3: Powershell Script to check the last connection time:
Asnp Citrix.*
$d = (Get-Date).AddDays(-60)
Get-BrokerDesktop -AdminAddress "$DDC" -DesktopGroupName "$DesktopGroup" -Filter { LastConnectionTime -le $d } | ft -a HostedMachineName,LastConnectionUser,LastConnectionTime,Protocol

Open in new window

Step 4: You need to enable maintenance mode of the VDI that you are planning to delete

Set-BrokerPrivateDesktop -AdminAddress "$DDC" -InMaintenanceMode $true -MachineName "$VDI"

Open in new window

Step 5: Shutdown the VM
This can be done using the PowerCli command or using the Citrix Powershell command

Using PowerCli command – VMWare

Get-VM ‘MachineName’| Shutdown-VMGuest -Confirm:$false

Open in new window

Using Citrix Powershell Command

New-BrokerHostingPowerAction -AdminAddress " $DDC" -MachineName " $VDI " -Action Shutdown

Open in new window

New-BrokerHostingPowerAction - Creates a new action in the power action queue.

Step 6: Delete the VDI from the Desktop Group 

Remove-BrokerMachine -AdminAddress "$DDC" -MachineName "$VDI” -DesktopGroup "$DesktopGroup" –Force

Open in new window

Step 7: Delete the VM from the Catalog

Remove-BrokerMachine -AdminAddress " $DDC " -MachineName "$VDI"

Open in new window

Remove-BrokerMachine  - Removes one or more machines from a desktop group.

Now we can combine all the codes and make it a script - save the file as filename.ps1
# Loading Snapins
Asnp Citrix.*
Add-PSSnapin VMware.VimAutomation.Core
#Declaring the Variables
$DDC = “DDC ServerName”
$VDI = “DomainName\VDI Name”
$DesktopGroup = “Desktop Group Name”

# Enable Maintenace Mode
Set-BrokerPrivateDesktop -AdminAddress "$DDC" -InMaintenanceMode $true -MachineName    "$VDI"

#Shutdown the VM
New-BrokerHostingPowerAction -AdminAddress " $DDC" -MachineName " $VDI " -Action Shutdown

#Delete the VDI from the Desktop Group  
Remove-BrokerMachine -AdminAddress "$DDC" -MachineName "$VDI” -DesktopGroup "$DesktopGroup" –Force

#Delete the VM from the Catalog
Remove-BrokerMachine -AdminAddress " $DDC " -MachineName "$VDI" 

Open in new window

Tested Environment
XenDesktop 5.6, PowerShell 3.0, Windows 7

Author:Rad David

Featured Post

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Join & Write a Comment

How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month