Introduction

In this blog, you will see how to create an Azure Storage Account using PowerShell.

Prerequisites

Install Azure PowerShell Module to run the script.

PowerShell Script

Open Notepad and paste the following script. Save the file as script.ps1.

  1. ################# Azure Blob Storage - PowerShell ####################
  2. ## Input Parameters
  3. $location="eastus" ## Get all the locations for your Azure Subscription: Get-AzLocation | select Location
  4. $resourceGroupName="azpractice"
  5. $storageAccName="azstorageacc1132020"
  6. ## Connect to Azure Account
  7. Connect-AzAccount
  8. ## Function to create Azure Storage Account
  9. Function CreateAzStorageAcc
  10. {
  11. ## Check if resource group exists
  12. if(Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue)
  13. {
  14. Write-Host -ForegroundColor Magenta $resourceGroupName "- resource group already exists."
  15. }
  16. else
  17. {
  18. Write-Host -ForegroundColor Magenta $resourceGroupName "- resource group does not exist."
  19. Write-Host -ForegroundColor Green "Creating th resource group - " $resourceGroupName
  20. ## Create a new resource group
  21. New-AzResourceGroup -Name $resourceGroupName -Location $location
  22. }
  23. ## Check if storage account exists
  24. if(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName -ErrorAction SilentlyContinue)
  25. {
  26. Write-Host -ForegroundColor Magenta $storageAccName "- storage account already exists."
  27. }
  28. else
  29. {
  30. Write-Host -ForegroundColor Magenta $storageAccName "- storage account does not exist."
  31. Write-Host -ForegroundColor Green "Creating the storage account - " $storageAccName
  32. ## Create a new Azure Storage Account
  33. New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName -Location $location -SkuName Standard_LRS
  34. }
  35. }
  36. CreateAzStorageAcc
  37. ## Disconnect from Azure Account
  38. Disconnect-AzAccount

Open the Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command.

.\script.ps1

Result

Azure Storage Account created successfully.

Summary

Thus, in this blog, you saw how to create an Azure Storage Account using PowerShell.