Guide to Uploading Files to Azure Blob Storage with PowerShell

Azure Blob storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data.

Use the Azure PowerShell module to create and manage Azure resources. You can create or manage Azure resources from the PowerShell command line or in scripts. This guide will help you to upload files/folders to the blob storage via script/PowerShell command.

  1. Install the latest version of Azure PowerShell for all users on the system in a PowerShell session opened with administrator rights using the following command:

    Install-Module -Name Az -AllowClobber -Scope AllUsers
  2. Use the following command and sign in to your Azure subscription when prompted:

  3. Get the storage account context to be used for the data transfer using the following commands:

$uploadstorage=Get-AzStorageAccount -ResourceGroupName <resource group name> -Name <storage account name>

Update the place holders <resource group name> and <storage account name> with values specific to your environment.

  1. Run the following command to upload a file from your local directory to a container in Azure storage:

    Set-AzStorageBlobContent -Container “<storage container name>” -File “<Location of file in local disk>” -Context $storcontext

    Replace the placeholders <storage container name> and <Location of file in local disk> with values specific to your environment

    Once the file is uploaded successfully, you will get a message similar to what you can see in the screenshot below:

    Graphical user interface, text, application

Description automatically generated

  2. To upload all files in the current folder, run the following command

    Get-ChildItem -File -Recurse | Set-AzStorageBlobContent -Container “<storage container name>” -Context $storcontext

  3. If you browse to the Azure storage container, you will see all the files uploaded.

    Graphical user interface, application

Description automatically generated

Here is the script code

<!-- wp:paragraph -->
<p>$uploadstorage=Get-AzStorageAccount -ResourceGroupName Storage-Group -Name <strong>StorageName</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Set-AzStorageBlobContent -Container "<strong>Name of Container</strong>" -File " <strong>Location of file in local disk</strong> " -Context $storcontext</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Copy and past in notepad save as script.ps1</p>
<!-- /wp:paragraph -->