Check if Azure Resource Group exists - Azure Powershell

I am trying to check if a ResourceGroup exists or not, so I thought that the following code should return true or false, but does not output anything.

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

Why am I not getting any output?

+13
powershell azure-resource-manager azure-powershell
source share
2 answers

There is a Get-AzureRmResourceGroup cmdlet :

 Get-AzureRmResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue if ($notPresent) { # ResourceGroup doesn't exist } else { # ResourceGroup exist } 

Note. Consider using the new Get-AzResourceGroup cmdlet :

 Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue if ($notPresent) { # ResourceGroup doesn't exist } else { # ResourceGroup exist } 
+17
source share

try it

 $ResourceGroupName = Read-Host "Resource group name" Find-AzureRmResourceGroup | where {$_.name -EQ $ResourceGroupName} 
+2
source share

All Articles