Reading Time: < 1 minute
Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

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
}
}
}

Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email
Categories: AzurePowerShell

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

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

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *