PowerShell script to create resource groups from csv file

Veeresh Kumar
2 min readOct 10, 2021

I was setting up lab environment on Azure, needed few resource groups to be created on different locations. Then, I thought lets create PowerShell script which is faster and automated way.

About:

It reads the csv file provided and creates the resource groups accordingly to name and location.

Here we go…!!

Pre-Requisites:

  • PowerShell 5.1 or later
  • Service Principal Name(SPN) and Secret or Username and Password to login to Azure
  • SPN/Username should have Administrator or Owner privileges to create resource groups on subscription
  • Azure Subscription Id
  • Azure Tenant Id
  • Az.Module ≥ 6.4.0

Note: The script supports both Username/Password and SPN/Secret login approach based on the $spnLogin parameter value.

If $spnLogin is $true : logins with SPN Credentials.

If $spnLogin is $false : logins with Username/Password.

Steps to create the resource groups:

  • Copy and Paste below scripts to files to az_create_resourcegroup.csv and az_create_resourcegroup.ps1 accordingly.
az_create_resourcegroup.ps1 script

Note: I have used comma(“,”) as delimiter in the file. The first row considered as Property Names (rgname,location) and you can pass the rgname and location as per your requirement in the csv file.

  • Now launch PowerShell as Administrator, Navigate to script file path.
  • Run the below command:

To login via Username/Password with $spnLogin value as $false:

login via Username/Password

To login via SPN/Secret with $spnLogin value as $true:

login via SPN/Secret
  • Once the command completes successfully, you can see the below output:
script execution output
  • You can verify the resource groups from azure portal as well.
resource group from portal

Hope this helps !! Happy Scripting !!

Thank You

--

--

Veeresh Kumar

He is a Sr. DevOps Engineer loves to automate cloud infrastructure and App deployments. #Terraform #Azure #Jenkins #Ansible #Chef #Bash #PowerShell #AWS #GCP