How to create a container in Azure Storage Account using PowerShell

In this article, you will see how to create a container in Azure Storage Account using PowerShell.


Install the Azure PowerShell Module:

Open Windows PowerShell window and run the below command.


Install-Module -Name Az –AllowClobber

Download Blob Contents:

Open a text file. Copy and paste the below script. Save the file as script.ps1.


################# Azure Blob Storage - PowerShell ####################

## Input Parameters

$resourceGroupName="psspschennairg"

$storageAccName="psspschennaistorageacc"

$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 Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command.


.\script.ps1


Result:

A new container created successfully as shown below.



Summary:

Thus in this article you saw how to create a container in Azure Storage Account using PowerShell.