public/Get-AzureEmptyResourceGroups.ps1


function Get-AzureEmptyResourceGroups {

    [cmdletbinding()]

    param (
    
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$subscriptionName
    
    )


    Process 
    {

        # check to see if local token exists (ran Login-AzureRMAccount)
        if (($null -eq (Get-AzureRmContext).Account)) {
            Write-Warning "Please run < Login-AzureRMAccount > first to create a session token...exiting."
            break
        } 

        Try {
            Select-AzureRmSubscription -SubscriptionName $subscriptionname -ErrorAction Stop -Verbose | Out-Null
        }
        Catch {
            $error[0].Exception
            break
        }

        $resources = Get-AzureRmResource
        $rgs = Get-AzureRmResource
        $emptyrgs = @()

        foreach ($r in $rg) {

            $validate = $Resources | Where-Object {$_.resourcegroupname -like $r.resourcegroupname}

            if ($validate.count -eq 0) {
                $emptyrgs += $r.resourcegroupname
            }
            elseif ($validate.count -ge 1) {
                Write-Verbose "$($r.ResourceGroupName) contains resources"
            }

        }

        if ($emptyrgs.count -ne 0) {

            Write-Warning "Empty resource group were found, returning..."
            return $emptyrgs
        }

    }

}