Jump to content
Welcome to our new Citrix community!
  • 1

Script to remove Multiple Desktops from Delivery Group and Machine catalogs

Jitendra Kumar


Hi All,


I need to remove multiple desktops from Delivery Groups and Machine catalogs. I am trying to prepare a code below and try to run it but its giving me some error.


Please can someone help me how to correct this code and what is missing pls.


asnp Citrix*
$vms = import-csv D:\Remove-VM\VMName.csv
$vms | foreach {

Remove-BrokerMachine -MachineName $_.Name -DesktopGroup $_.GroupName


Input file....



Link to comment

1 answer to this question

Recommended Posts

  • 0

Here is a snippet of code that may help you:

        If (Get-BrokerMachine -HostedMachineName $Machine) {
            $session = Get-BrokerSession -HostedMachineName $Machine
            If ($session) {
                $session | Stop-BrokerSession
                Do {Write-Host (Get-Date) "Waiting for session to end on" $Machine -foregroundcolor "green" ; Sleep -s 3 ; $session = Get-BrokerSession -HostedMachineName $Machine}
                    While ($Session)
            # Place in maintenance mode
            $brokerDesktop = Get-BrokerDesktop -HostedMachineName $machine
            Set-BrokerMachine $brokerDesktop.MachineName -InMaintenanceMode $true
            # Shutdown and wait until powered off
            if ($brokerDesktop.PowerState -eq "On") {
                $trash = New-BrokerHostingPowerAction -Action Shutdown $brokerDesktop.MachineName
                Do {Write-Host (Get-Date) "Waiting for" $Machine "to power down" -foregroundcolor "green" ; Sleep -s 5 ; $brokerDesktop = Get-BrokerDesktop -HostedMachineName $machine}
                    While ($brokerDesktop.PowerState -eq "On")
            # Remove machine from desktop group and machine catalog
            Write-Host "Removing" $Machine "from Delivery Group -" $brokerDesktop.DesktopGroupName -foregroundcolor "green"
            Remove-BrokerMachine -MachineName $brokerDesktop.MachineName -DesktopGroup $brokerDesktop.DesktopGroupName
            Write-Host "Removing" $Machine "from machine catalog -" $brokerDesktop.CatalogName -foregroundcolor "green"
            Remove-BrokerMachine -MachineName $brokerDesktop.MachineName
            $RemovedMachines = $RemovedMachines + $Machine


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...