Automate Azure Blob Storage with PowerShell

Automate Azure Blob Storage :: Upload File from PowerShell ISEMicrosoft Azure Storage provides several different storage services. Arguably the most common storage service is called blob storage. Azure Blob Storage is very similar to Amazon Web Services’ (AWS) Simple Storage Service (S3). AWS S3 offers buckets and objects, whereas Microsoft Azure offers a single layer of “blob containers” each of which can hold many blobs. In this post, we’ll explore how to automate Azure Blob Storage, using the PowerShell module available from Microsoft!

Automate Azure Blob Storage

If you’re an IT infrastructure engineer or a software developer, you’ll be able to make good use of Azure blob storage. For example, as an infrastructure engineer you could use blob storage to maintain an off-site, cloud-based backup of your infrastructure and applications. You might also want to upload custom VHD images of your on-premises virtual machines, so you can migrate those workloads to the cloud. If you’re a web application developer, you might use Azure blob storage to host static files, such as images or video, or any other kind of data. In any case, you can use the Azure PowerShell module to upload your data to your Azure subscription, and make it accessible to cloud applications and infrastructure services.

In the rest of this article, we’ll explore how to use the Azure PowerShell module to create a Storage Account, create a Blob Container in the Storage Account, and upload files on our local filesystem to it! You’ll need to make sure that you have a valid Azure subscription before proceeding.

Installing Azure PowerShell

The first thing you need to do is install the Azure PowerShell module. If you’re not running Windows 10 already, then make sure you upgrade your down-level operating system to PowerShell version 5.0. It isn’t an absolute requirement to have PowerShell 5.0, but it makes installation of the Azure PowerShell module much easier! Now that you’ve got PowerShell v5, to install the Azure PowerShell module, simply run the following command: Install-Module -Name AzureRM -Scope CurrentUser. This command will install the Azure Resource Manager (ARM) PowerShell module from the PowerShell Gallery, which is a central, publicly accessible repository for modules and scripts.

Create an ARM Resource Group

Now that we’ve installed the Azure PowerShell module, let’s start by creating a Resource Group to contain our Storage Account. We authenticate to Microsoft Azure using the Add-AzureRmAccount command, and then create a Resource Group using New-AzureRmResourceGroup. In the example below, we are using a simple technique, called PowerShell Splatting. Watch the video below to learn more about PowerShell Splatting.

We need to create a Resource Group inside our Azure Subscription, because the Azure Resource Manager (ARM) API requires that all cloud resources be a member of a Resource Group. You can think of a Resource Group like a management container. For more information about Azure Resource Manager and Resource Groups, check out our video-based training course that focuses on ARM.

### Authenticate to Microsoft Azure using Microsoft Account (MSA) or Azure Active Directory (AAD)

### Create an Azure Resource Manager (ARM) Resource Group
$ResourceGroup = @{
Name = 'ArtofShell';
Location = 'West Europe';
Force = $true;
New-AzureRmResourceGroup @ResourceGroup;

Create a Storage Account

Now that we’ve created an ARM Resource Group (the management container), we can create a Storage Account. A Storage Account is a cloud resource that acts as our gateway into the entire Azure Storage service. To create a Storage Account, we use the aptly-named New-AzureRmStorageAccount command. We need to specify a name for the Storage Account, as well as the Resource Group that we want to provision it into. We’ll use the Resource Group that we just created in the last step. The name of the Storage Account itself needs to be globally unique. If someone else has already taken the name that you want to use, then you’ll need to choose a different one. Also, keep in mind that Azure Storage Account resources cannot have capital letters in their names, and cannot start with a number.

$StorageAccount = @{
    ResourceGroupName = 'ArtofShell';
    Name = 'artofshellstorage';
    SkuName = 'Standard_LRS';
    Location = 'West Europe';
New-AzureRmStorageAccount @StorageAccount;

Authenticate to the Azure Blob Storage Service

Now that we’ve created the Storage Account, we need to start communicating with the Azure Storage REST API, of course, using PowerShell. This requires us to create an authentication context that specifically targets the Azure Storage service. At this point, we are shifting away from communicating with the Azure Resource Manager (ARM) API, and talking to Azure Storage instead. First, we’ll obtain the Storage Account keys using ARM, and then we’ll create a Storage Context object using one of the keys. The Storage Context object itself is what enables us to authenticate to the Azure Storage REST API from PowerShell. You’ll notice that the command names no longer have “AzureRm” in the name, and instead simply have a noun prefix of “Azure.” That’s because the Azure Storage commands aren’t part of the Azure Resource Manager interface.

### Obtain the Storage Account authentication keys using Azure Resource Manager (ARM)
$Keys = Get-AzureRmStorageAccountKey -ResourceGroupName ArtofShell -Name artofshellstorage;

### Use the Azure.Storage module to create a Storage Authentication Context
$StorageContext = New-AzureStorageContext -StorageAccountName ArtofShell -StorageAccountKey $Keys[0].Value;

Create a Blob Container

Now that we’re authenticated to the Azure Storage service, using the Storage Context object, we need to create a Blob Container. A Blob Container holds zero or more storage “blobs.” Blob Containers cannot be nested together, so keep in mind that only one level of Blob Containers is supported. To create a Blob Container, we’ll call the New-AzureStorageContainer command, similar to the example below. We need to pass in the Storage Context object into the -Context parameter. We’ll also specify a name for the container, using the -Name parameter.

### Create a Blob Container in the Storage Account
New-AzureStorageContainer -Context $StorageContext -Name uploads;

Upload a Blob

The last step in this process is to upload a file from our local filesystem to the Blob Container. The Set-AzureStorageBlobContent command is what we need to call, in order to make this happen. For input parameters, we just need to specify the Storage Context object, the Blob Container name, and the local file that we want to upload into it. Of course, you can specify your own file path for the -File parameter. In this particular example, we are simply uploading the Windows Notepad executable file as an example.

### Upload a file to the Microsoft Azure Storage Blob Container
$UploadFile = @{
    Context = $StorageContext;
    Container = 'uploads';
    File = "$env:WinDir\notepad.exe";
Set-AzureStorageBlobContent @UploadFile;


In this article, we’ve explored how to upload files from our local filesystem into the Microsoft Azure Blob Storage service, using PowerShell! First, we authenticated to Azure, then we created a Resource Group, created a Storage Account, created a Blob Container, and then finally uploaded a file to the Blob Container! You can use this technique to automate Azure Blob Storage and manage your cloud storage effectively and efficiently.

This article first appeared on the Art of Shell blog. Would you like to learn more about the Microsoft Azure Blob Storage service, from the ground up? Check out our video-based training course on Microsoft Azure Storage!

Leave a Reply

Your email address will not be published. Required fields are marked *