Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use PowerShell to find and delete empty resource groups in all Azure subscriptions

Shulou Source: shulou.com Published: 2022-06-03 05:01:10 10月04日 Update

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}

Tags: Resources scripts accounts subscriptions different situation useful faceted help management Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi MariaDB macOS vpn