Use PowerShell to find and delete empty resource groups in all Azure subscriptions
In many cases, we forget to delete Azure resource groups when we are done with them, so I created the following PowerShell script to help clean them up. It applies to all account subscriptions. This script is useful if you have a lot of people in your account managing different Azure Resource but not cleaning up empty resource groups themselves.
# Login to Azure accountLogin-AzureRmAccount # GlobalLogin-AzureRmAccount-Environment AzureChinaCloud # 21V#Get list of Azure Subscription ID's$Subs = (get-AzureRMSubscription) .ID # Loop through the subscriptions to find all empty Resource Groups and store them in $EmptyRGsForEach ($sub in $Subs) {Select-AzureRmSubscription-SubscriptionId $Sub$AllRGs = (Get-AzureRmResourceGroup). ResourceGroupName$UsedRGs = (Get-AzureRMResource | Group-Object ResourceGroupName). Name$EmptyRGs = $AllRGs | Where-Object {$_ notin $UsedRGs} # Loop through the empty Resorce Groups asking if you would like to delete them. And then deletes them.foreach ($EmptyRG in $EmptyRGs) {$Confirmation = Read-Host "Would you like to delete $EmptyRG'(Y) es' or'(N) o'" IF ($Confirmation-eq "y"-or $Confirmation-eq "Yes") {Write-Host "Deleting" $EmptyRG "ResourceGroup" Remove-AzureRmResourceGroup-Name $EmptyRG-Force}