How to check if the Azure resource group is empty or not using PowerShell?


To check if the resource group is empty or not we need to check if the resource group contains any resources.

For this example, We have a resource group name called  the TestRG and we need to check if it is empty.

Example

$resources = Get-AzResource -ResourceGroupName TestRG
if($resources){"Resource group is not empty"}
else{"Resource group is empty"}

Output

Resource group is empty

To check if the resource groups in the particular subscription are empty or not, use the below code.

Output

Connect-AZAccount
Set-AzContext -SubscriptionName 'Your Subscription Name'
$rgs = Get-AzResourceGroup

Write-Output "Empty Resource Groups"
foreach($rg in $rgs.ResourceGroupName){
    $resources = Get-AzResource -ResourceGroupName $rg
    if(!($resources)){ $rg }
}

Updated on: 12-Apr-2021

944 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements