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

PowerShellMicrosoft TechnologiesSoftware & Coding

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 }
}
raja
Published on 12-Apr-2021 11:10:14
Advertisements