Create Azure Storage Account And Container Using PowerShell

I’ve been learning Azure for a while and experimenting with various aspects of the Azure Cloud world. Storage Account is one of the potential areas which can be used to keep the information in form of the following.

  1. Azure Blob
  2. Azure File
  3. Azure Queues
  4. Azure Tables

An excerpt about Azure Storage - reference https://docs.microsoft.com/en-us/azure/storage/common/storage-introduction - "Azure Storage is Microsoft's cloud storage solution for modern data storage scenarios"

Azure Storage offers a massively scalable object store for data objects, a file system service for the cloud, a messaging store for reliable messaging, and a NoSQL store. Azure Storage is -
  1. Durable and highly available
    Redundancy ensures that your data is safe in the event of transient hardware failures. You can also opt to replicate data across data centers or geographical regions for additional protection from local catastrophe or natural disaster. Data replicated in this way remains highly available in the event of an unexpected outage.

  2. Secure
    All data written to Azure Storage is encrypted by the service. Azure Storage provides you with fine-grained control over who has access to your data.

  3. Scalable
    Azure Storage is designed to be massively scalable to meet the data storage and performance needs of today's applications.

  4. Managed
    Microsoft Azure handles maintenance and any critical problems for you.

  5. Accessible
    Data in Azure Storage is accessible from anywhere in the world over HTTP or HTTPS. Microsoft provides SDKs for Azure Storage in a variety of languages -- .NET, Java, Node.js, Python, PHP, Ruby, Go, and others -- as well as a mature REST API. Azure Storage supports scripting in Azure PowerShell or Azure CLI. And the Azure portal and Azure Storage Explorer offer easy visual solutions for working with your data.

There are generally the following ways to create a Storage Account in Azure.

  1. Through Azure Resource Manager portal https://portal.azure.com
  2. Using PowerShell commands or cmdlet

In this article, I’ll be creating an Azure Storage account through Powershell commands, which is very easy to write. In a few minutes, you will get the complete storage account along with the Container and Resource group without jumping into Portal.

I believe this approach is useful especially in the professional world because you can create plenty of resources within the ARM portal through PowerShell in a very less time.

Prerequisites

  1. You should have Powershell ISE installed on your workstation.
  2. You should be an authenticated user on Azure Portal.
  3. You should possess a basic understanding of Azure.

Note
In Windows 10, PowerShell ISE comes by default, as shown below.

Create Azure Storage Account and Container using PowerShell 

As soon as you open this, you will have the following window.

Create Azure Storage Account and Container using PowerShell 

Now, it's time to execute the following command in the PowerShell Editor. You can execute all of them in a single go or can execute one by one.

Step 1 

Execute the command - Add-AzureAccount

It will prompt the following window. Put your credentials like username and password as depicted in the below windows.

Create Azure Storage Account and Container using PowerShell 
 
Create Azure Storage Account and Container using PowerShell
 
Create Azure Storage Account and Container using PowerShell 

Step 2

Execute the following command in order to create a Storage Account using the Run Selection (F8). Kindly refer to the image below for the command line.

New-AzureStorageAccount -StorageAccountName "dotnetpiperstorage" -Description "dotnetpiperstorage" -Location "East US" -Type Standard_LRS

Create Azure Storage Account and Container using PowerShell 

Step 3

Execute the following command. 
 
Get-AzureStorageAccount

The above command confirms and returns the details of a recent storage account.

Create Azure Storage Account and Container using PowerShell 

Step 4

Execute the following command- 
Get-AzureStorageKey -StorageAccountName "dotnetpiperstorage"
 
Create Azure Storage Account and Container using PowerShell 

Step 4

Execute the following commands -

  1. $storagekey = (Get-AzureStorageKey -StorageAccountName "dotnetpiperstorage").Primary  
  2. $storagecontext = New-AzureStorageContext -StorageAccountName "dotnetpiperstorageContainer" -StorageAccountKey $storagekey  
  3. $container = New-AzureStorageContainer -Name "dotnetpiperstorageContainer" -Permission Container -Context $storagecontext  

Note
Any keyword created along with “$” sign is considered local variable and retains in the same session.

Step5

After creating the context and container sucessfuly, you can verify the follwoing cmdlet.
 
Create Azure Storage Account and Container using PowerShell 
  1. Get - AzureStorageContainer - Context $storagecontext  
  2. Add - AzureAccount  
  3. New - AzureStorageAccount - StorageAccountName "dotnetpiperstorage" - Description "dotnetpiperstorage" - Location "East US" - Type Standard_LRS  
  4. Get - AzureStorageAccount  
  5. Get - AzureStorageKey - StorageAccountName "dotnetpiperstorage"  
  6. New - AzureStorageKey - KeyType Primary - StorageAccountName "dotnetpiperstorage"#  
  7. to create Storage Container  
  8. $storagekey = (Get - AzureStorageKey - StorageAccountName "dotnetpiperstorage").Primary  
  9. $storagecontext = New - AzureStorageContext - StorageAccountName "dotnetpiperstorage" - StorageAccountKey $storagekey  
  10. $container = New - AzureStorageContainer - Name "dotnetpiperstoragecontainer" - Permission Container - Context $storagecontext  
  11. Get - AzureStorageContainer - Context $storagecontext  

Now, you can also jump into Azure Portal to see the stuff you have created in the last few steps.

Open - https://portal.azure.com -> All Services ->Storage Account

Create Azure Storage Account and Container using PowerShell 

Click on Storage Account. It will take you on another screen, which has potential information about all the storage accounts available within subscription as depicted below.

Create Azure Storage Account and Container using PowerShell 

Click on dotnetpiperstorage. It will take you the following screen which has detailed information about Storage Account.

Create Azure Storage Account and Container using PowerShell 

Click on the blobs as shown on the above window to see the container which we’ve created recently using the above step.

Create Azure Storage Account and Container using PowerShell 

That’s it.

Note
Azure Storage forces you to keep the name of the storage account in the lower font as well as for the container. Rather than jumping into ARM portal, you can easily create Azure Storage Account with these few commands.


Similar Articles