Check If Azure Resource Group Exist - Azure Powershell

Peter Pavelka picture Peter Pavelka · Jun 2, 2016 · Viewed 13.9k times · Source

I'm trying to verify if ResourceGroup exist or not so i thought that following code should return true or false, but it doesn't output anything.

$RSGtest = Find-AzureRmResource | Format-List ResourceGroupName | get-unique
$RSGtest -Match "$myResourceGroupName"

Why am I not getting any output?

Answer

Martin Brandl picture Martin Brandl · Jun 2, 2016

Update:

You should use the Get-AzResourceGroup cmdlet from the new cross-plattform AZ PowerShell Module now. :

Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($notPresent)
{
    # ResourceGroup doesn't exist
}
else
{
    # ResourceGroup exist
}

Original Answer:

There is a Get-AzureRmResourceGroup cmdlet:

Get-AzureRmResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($notPresent)
{
    # ResourceGroup doesn't exist
}
else
{
    # ResourceGroup exist
}