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:

    Connect-AzAccount
  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 -->
<p>$storcontext=$uploadstorage.Context</p>
<!-- /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 -->

Leave a Reply

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

Chirag Shah
Chirag Shah

Chirag shah is Network and system engineer and has been associated with Infrassist for last 6 months now. Chirag has played an important role in studying and assessing needs, proposing potential solutions, and guaranteeing customer satisfaction by providing the needed Support. Chirag has experience in Windows server, Patch management, backups, Microsoft Intune, Azure AD, Microsoft 365 and more.

Thanks For Reading