Create StorageAccounts Using PowerShell

Here, we will see how to automate the creation of storage accounts using PowerShell. We are using Cloud Shell. The intent is to understand the script.

Pre Requisites

  • An Azure account with elevated privileges.
  • Install Azure PowerShell Modules from here.
  • Otherwise, use Azure CloudShell from Azure Portal.

Script

To define the underlying variables needed, we need to define a resource Group name in which the storage account exists and the desired location.

$resourceGroupName = "AZ-140"
$location = "East US"

Using for loop to create storage accounts. Loop to create 10 storage accounts

for ($i = 1; $i -le 10; $i++) {
$storageAccountName = “strgdemoacc$i”

$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -Location $location -SkuName Standard_LRS

Write-Output “Created storage account: $storageAccountName”

}

Explanation:

  • For loop will do three things. First, it will define all the storage account names here. $storageAccountName = “strgdemoacc$i”
  • Second, $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -Location $location -SkuName Standard_LRS, this command will create all storage accounts defined in the first bullet.
  • Once the loop does its job, it will output a message stating “Created storage account: $storageAccountName” 10 times with the storage account name created.

Output:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top