Add to existing Resource Group if it exists, otherwise, create new Resource group RRS feed

  • Question

  • Hi,

    I'm using Azure Powershell to automate some VM deployments. 

    Currently, I'm using the following lines to create a new Resource Group:

    $RGName = Read-Host 'Enter NEW Resource Group Name'
    $RG = New-AzureRmResourceGroup -ResourceGroupName $RGName -Location $Location

    This works fine when the Resource Group doesn't exist, but as we continue our use of Azure, we need to be able to add resources to existing Resource Groups. My question is, is there a way to evaluate existing RGs, and if the user enters an existing RG, put the resource into that RG rather than throw an error saying the RG already exists?


    Thursday, May 10, 2018 11:37 PM

All replies

  • Get the resource group first and if it doesn't exist create it.


    Friday, May 11, 2018 12:20 AM
  • Yes, I understand that.  But how do I do that?

    I got to

    Get-AzureRMResourceGroup | ft resourcegroupname,location

    That gives me the list of RGs, but how can i get powershell to either add to an existing or create a new RG?

    Friday, May 11, 2018 12:22 AM
  • Just ask for it by name:

    Get-AzureRmResourceGroup $RGName


    Friday, May 11, 2018 12:26 AM
  • How does it compare what a user enters to what exists?  

    What i'm trying to accomplish is to automate the process of adding resources into RGs.  If a user has to add multiple resources to a few different RGs, I don't want them to have to look through the entire list of RGs to see if it already exists.  I want to automate it so the user can enter MyResourceGroup for $RGName and have the script figure out if it has to create it, or just add the resource to MyResourceGroup.


    Friday, May 11, 2018 12:40 AM
  • It doesn't compare anything.  If you can get the group it exists.  If not then create it.

    Before you hurt yourself trying to guess at how programming works I suggest you start by learning basic PowerShell.  All of your questions would then be answered.


    Learn PowerShell  
    PowerShell Documentation
    PowerShell Style Guidelines


    Friday, May 11, 2018 1:00 AM