This is a blogpost about a script which initiates a delete request for all resources in a Resource Group, I use this script for deleting WVD hostpools with Powershell.
The hostpool resource need to be deleted in an exact order.
1. VM’s need to be deleted first.
2. After that sessionhosts must delete from the hostpool.
3. Then all the resource except for the hostpool are deleted.
4. Lastly, the hostpool is deleted.
Script Syntaxis
This is the syntaxis for the script. In addition, please remember to edit the array “$ResourceGroupname”.
$ResourceGroupName = "ResourceGroupName" $ExistingHostPool = Get-AzResource -ResourceGroupName $ResourceGroupName | Where-Object ResourceType -eq Microsoft.DesktopVirtualization/hostpools #VM's need to be deleted first. $HostpoolResourcesVMs = Get-AzResource -ResourceGroupName $ResourceGroupName | Where-Object ResourceType -eq Microsoft.Compute/virtualMachines foreach ($HostpoolResourceVM in $HostpoolResourcesVMs){ Write-Host Deleting $HostpoolResourceVM.Name Remove-AzResource -Resourceid $HostpoolResourceVM.ResourceId -force } # After that sessionhosts must be delete from Hostpool foreach ($Hostpool in $ExistingHostPool) { $WVDSessionhosts = Get-AzWvdSessionHost -HostPoolName $Hostpool.Name -ResourceGroupName $ResourceGroupName foreach ($WVDSessionHost in $WVDSessionhosts) { $InputString = $WVDSessionhost.Name $WVDArray = $InputString.Split("/") $WVDArray[0] $WVDArray[1] Write-host Removing $WVDArray[1] from Hostpool $HostPool.Name Remove-AzWvdSessionHost -HostPoolName $HostPool.Name -ResourceGroupName $ResourceGroupName -Name $WVDArray[1] } } #Now it is time to delete all resources except for the hostpool $HostpoolResources0thers = Get-AzResource -ResourceGroupName $ResourceGroupName | Where-Object ResourceType -ne Microsoft.DesktopVirtualization/hostpools foreach ($HostpoolResources0ther in $HostpoolResources0thers){ Write-Host Deleting $HostpoolResources0ther.Name Remove-AzResource -Resourceid $HostpoolResources0ther.ResourceId -force } #Lastly delete the host pool foreach($Hostpool in $ExistingHostPool){ if (($ExistingHostPool).count -gt "0") { Write-Host "Removing Windows Virtual Desktop Hostpool $HostPool.Name" Remove-AzWvdHostPool -Name $HostPool.Name -ResourceGroupName $ResourceGroupName } }
Example
Firstly, check whether you have correct Powershell modules installed. Use the following code to do so:
$installedPackageProvider = Get-PackageProvider if ($installedPackageProvider.Name -notmatch "NuGet") { Install-PackageProvider -Name NuGet -force Write-Host("Install powershell module NuGet") } $installedModules = Get-InstalledModule if ($installedModules.Name -notmatch "Az.Accounts") { Install-Module Az.Accounts -Force -AllowClobber Write-Host("Install powershell module Az Accounts") } if ($installedModules.Name -notmatch "Az.Resources") { Install-Module Az.Resources -Force -AllowClobber Write-Host("Install powershell module Az Resources") } if ($installedModules.Name -notmatch "Az.DesktopVirtualization") { Install-Module Az.DesktopVirtualization -Force -AllowClobber Write-Host("Install powershell module Az DesktopVirtualization") }
Secondly, you need to connect to Azure Powershell. You can do so by using the following command:
Connect-AzAccount
Firstly, save the script syntaxis as a .ps1 file. After that, open up Powershell and browse to the location where you placed the script.
Now it’s time to run the script. Save the script as a ps1 file and run it:
.\DeleteHostPoolscript.ps1
This is the output for the script. As you can see the order mentioned in the first paragraph is shown:

And that is how you delete WVD hostpools with a Powershell script!
References
My other WVD scripts:
Logoff Users from a WVD Hostpool
Set Drain Mode on a WVD Hostpool
Microsoft Docs about WVD Powershell
[…] https://www.nielskok.tech/windows-virtual-desktop/wvd-delete-hostpools-powershell/ […]
[…] Niels Kok who seems to be on a roll with his blog posting, has created another great PowerShell script which will delete a WVD Host pool via PowerShell. It’s a bit of a pain doing it manually as there are a few steps to go though, Niels has now scripted this so thank you Niels! You can read his post here – https://www.nielskok.tech/windows-virtual-desktop/wvd-delete-hostpools-powershell/ […]
[…] My Other WVD Scripts:Drain HostpoolsLogOff UsersDelete Hostpools […]
[…] Microsoft Docs about removing NSG RulesWVD Script about Refresh TokenWVD Script about Deleting HostPools […]
[…] WVD Delete hostpool scriptFlow triggered DevOps BuildOneDrive Microsoft Docs […]