I am trying to find all the Azure RM resource groups with no resources in it and delete those resource groups using PowerShell. Deleting using Portal is so time consuming. Using powershell I was able to accomplish by using the following code. Is there a better way of achieving this in powershell?
$allResourceGroups = Get-AzureRmResourceGroup
$resourceGroupsWithResources = Get-AzureRMResource | Group-Object ResourceGroupName
$allResourceGroups | % {
$r1 = $_
[bool]$hasResource = $false
$resourceGroupsWithResources | % {
if($r1.ResourceGroupName -eq $_.Name){
$hasResource = $true
}
}
if($hasResource -eq $false){
Remove-AzureRmResourceGroup -Name $r1.ResourceGroupName -Force
}
}
You could try
Here they are packaged as functions that can be called