I am forever forgetting to delete Azure Resource Groups after I have finished with them, so I created the little PowerShell script below to help me clean them up. It works for all of your accounts Subscriptions too. This script could come in handy if multiple people are managing your Azure estate and not cleaning up after themselves.
I hope you find this script helpful. If you have any comments or questions please reach out.
The Script
#Log in to Azure account | |
Login-AzureRmAccount | |
#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 $EmptyRGs | |
ForEach ($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 "Resource Group" | |
Remove-AzureRmResourceGroup -Name $EmptyRG -Force | |
} | |
} | |
} |
1 Comment
Orgad · January 16, 2023 at 8:40 pm
Thank you! I posted an updated version as a comment to your gist: https://gist.github.com/PixelRobots/8f539e805b2618eae17026c7c48541eb?permalink_comment_id=4438852#gistcomment-4438852