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.
- ################# Azure Blob Storage - PowerShell ####################
-
- ## Input Parameters
- $resourceGroupName="azpractice"
- $storageAccName="azstorageacc1122020"
- $storageContainerName="container001"
-
- ## Connect to Azure Account
- Connect-AzAccount
-
- ## Function to create the storage container
- Function CreateStorageContainer
- {
- Write-Host -ForegroundColor Green "Creating storage container.."
- ## Get the storage account in which container has to be created
- $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName
- ## Get the storage account context
- $ctx=$storageAcc.Context
-
- ## Check if the storage container exists
- if(Get-AzStorageContainer -Name $storageContainerName -Context $ctx -ErrorAction SilentlyContinue)
- {
- Write-Host -ForegroundColor Magenta $storageContainerName "- container already exists."
- }
- else
- {
- Write-Host -ForegroundColor Magenta $storageContainerName "- container does not exist."
- ## Create a new Azure Storage Account
- New-AzStorageContainer -Name $storageContainerName -Context $ctx -Permission Container
- }
- }
-
- CreateStorageContainer
-
- ## Disconnect from Azure Account
- 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.