Introduction

In this blog, you will see how to create a container in 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. $resourceGroupName="azpractice"
  4. $storageAccName="azstorageacc1122020"
  5. $storageContainerName="container001"
  6. ## Connect to Azure Account
  7. Connect-AzAccount
  8. ## Function to create the storage container
  9. Function CreateStorageContainer
  10. {
  11. Write-Host -ForegroundColor Green "Creating storage container.."
  12. ## Get the storage account in which container has to be created
  13. $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName
  14. ## Get the storage account context
  15. $ctx=$storageAcc.Context
  16. ## Check if the storage container exists
  17. if(Get-AzStorageContainer -Name $storageContainerName -Context $ctx -ErrorAction SilentlyContinue)
  18. {
  19. Write-Host -ForegroundColor Magenta $storageContainerName "- container already exists."
  20. }
  21. else
  22. {
  23. Write-Host -ForegroundColor Magenta $storageContainerName "- container does not exist."
  24. ## Create a new Azure Storage Account
  25. New-AzStorageContainer -Name $storageContainerName -Context $ctx -Permission Container
  26. }
  27. }
  28. CreateStorageContainer
  29. ## Disconnect from Azure Account
  30. 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:

Container created successfully.

Reference

https://docs.microsoft.com/en-us/powershell/module/az.storage/new-azstoragecontainer?view=azps-3.3.0

Summary

In this blog, you saw how to create a container in Azure Storage Account using PowerShell.