Check If Azure Resource Group Exist - Azure Powershell Check If Azure Resource Group Exist - Azure Powershell powershell powershell

Check If Azure Resource Group Exist - Azure Powershell


Update:

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

Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinueif ($notPresent){    # ResourceGroup doesn't exist}else{    # ResourceGroup exist}

Original Answer:

There is a Get-AzureRmResourceGroup cmdlet:

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


try this

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


I was also looking for the same thing but there was a additional condition in my scenario.

So I figured it out like this. To get the scenario details follow

$rg="myrg"$Subscriptions = Get-AzSubscription$Rglist=@()foreach ($Subscription in $Subscriptions){$Rglist +=(Get-AzResourceGroup).ResourceGroupName}$rgfinal=$rg$i=1while($rgfinal -in $Rglist){$rgfinal=$rg +"0" + $i++}Write-Output $rgfinalSet-AzContext -Subscription "Subscription Name"$createrg= New-AzResourceGroup -Name $rgfinal -Location "location"