Create an Azure Storage Blob Container with PowerShell

My observations so far with the Azure PowerShell experience have been somewhat mixed and the example in this post will give you a flavour of that. I wanted to create a new Storage Blob Container via PowerShell, rather than through the below process in the web portal:

I looked for cmdlets which could potentially be used:

However, it returned nothing from the AzureRM module, only the Azure module. (There are currently two modules you need to use when working with Azure, some more info here and here) To say this can get confusing when you are new to the topic is an understatement, hopefully this situation is going to improve significantly ASAP.

So it looks like I need to use New-AzureStorageContainer from the original Azure module, however there do not appear to be any examples which show you how to add it into the desired place, i.e. Resource Group and Storage Account:

So far I have found two different ways to get this done:

1)Set the current Storage Account

I found a StackOverflow post with an example. You need to first of all call a cmdlet from the AzureRM module to set the current Storage Account (note line 2 is the weird response you get from running the command in line 1, i.e. just a string with the name of the current Storage Account, not an object representing it):

Now I can use New-AzureStorageContainer and it will get created in the correct place:

2) Use Storage Account Keys

Within a Storage Account are two Access keys which can be used for automation:

We only need one of the keys, but the following will retrieve both and then we pick out the first key value:

Now using one of the key values we can set the Storage Context:

Note: the above doesn’t actually seem to perform any validation on whether a Storage Account with that name exists. I initially had a typo in  the name and when using the next command generated the error: New-AzureStorageContainer : The remote name could not be resolved: ‘’

Now if we have used the correct name for an existing Storage Account we can create the Storage Container using the generated Storage Context:

Please leave a comment if I have missed an easier way to do it, I’d love to know 🙂

2 thoughts on “Create an Azure Storage Blob Container with PowerShell

  1. I’m a major fan of the pipe to outputGrid that then captures the select result in a variable :).
    Azure don’t make it easy with 2 completely seperate command sets: AzureRM, and Azure.
    Make sure you have the latest Azure powershell version installed:

    Here is a simple script I wrote (should be annotated enough for easy reading):
    #Login to AzureRM
    #Select Subscription
    $subscriptionId = ( Get-AzureRmSubscription | Out-GridView -Title “Select an Azure Subscription …” -PassThru).SubscriptionId
    #Open selected subscription
    Select-AzureRmSubscription -SubscriptionId $subscriptionId
    # Register ResourceProviders
    $resourceProviders = @(“microsoft.compute”,””,”microsoft.sql”,””)
    Write-Host “Registering resource providers”
    foreach($resourceProvider in $resourceProviders) {

    #Select resource Group
    $resourceGroup = ( Get-AzureRmResourceGroup |Out-GridView -Title “Select an Azure Resource Group …” -PassThru)

    $storageAccountNameInput = read-host “Please enter a storage account name (all lowercase)”
    $storageAccount = Get-AzureRmStorageAccount -Name $storageAccountNameInput -ResourceGroup $resourceGroupName -ErrorAction SilentlyContinue
    $createStorageAccount = Read-Host “Account doesn’t exist create it? (Y/N)”
    if($createStorageAccount -eq “Y”){
    Write-Host “Creating storage account: $storageAccountNameInput”
    $storageAccount = New-AzureRMStorageAccount –StorageAccountName $newStorageAccountName -Location $resourceGroup.Location -ResourceGroupName $resourceGroup.Name -SkuName Standard_LRS
    $storageAccountKey0 = (Get-AzureRmStorageAccountKey -Name $storageAccount -ResourceGroupName $resourceGroup.Name).Value[0]
    $storageAccountContext = New-AzureStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageAccountKey0
    $createBlob = Read-Host “Would you like to create a BlobContainer? (Y/N)”
    if($createBlob -eq “Y”){
    $newBlobName = Read-Host “Please enter the blob name: (all lowercase)”
    Write-Host “Creating Blob container: $newBlobName”
    New-AzureStorageContainer -Name $newBlobName -Permission Off -Context $storageAccountContext

    Other useful code to pre-populate Blobs with data from another blob:
    # Copy the blob #
    $blobCopy = Start-AzureStorageBlobCopy -DestContainer $destinationContainerName -DestContext $destinationContext -SrcBlob $blobName -DestBlob “v1ubuntudocker.vhd” -Context $sourceContext -SrcContainer $sourceContainer

    ### Retrieve the current status of the copy operation ###
    $status = $blobCopy | Get-AzureStorageBlobCopyState

    #Set start transfer rate value
    $ratestart = $status.BytesCopied
    ### Loop until complete ###
    While($status.Status -eq “Pending”){
    $percent = ($status.BytesCopied / $status.TotalBytes) * 100
    #Generate rate as GB/min
    $transRate = ($status.BytesCopied – $ratestart) / 1048576
    ### Print out status ###
    $outputText = “Copy still in progress /r/nStatus: ” + $status.Status + “, Percent: ” + $percent + “, Rate: ” + $transRate + “GB/min”
    Write-Host $outputText
    #60s wait timer
    Start-Sleep -s 60
    #Get updated status
    $status = $blobCopy | Get-AzureStorageBlobCopyState
    $ratestart = $status.BytesCopied
    #final copy output
    $outputText = “Copy completed /r/nStatus: ” + $status.Status
    Write-Host $outputText

    All the above has been actively used to pre-create and populate a custom VHD image blob before then firing up a fully scripted Azure JSON deployment deploying mulitple VM’s from the root image.
    This was between Multiple regions, hence the additional while loop for monitoring the copy status.
    As we were originally not checking, and getting VM deploy errors with the VHD in pending state.

Comments are closed.