How to Upload Blob Contents in an Azure Storage Account using PowerShell

Introduction 

In this blog, you will see how to upload blob contents in an Azure Storage Account using PowerShell.

Prerequisites:

Install Azure PowerShell Module to run the script.

PowerShell Script:

Open Notepad and paste the following script. Save the file as script.ps1.

  1. ################# Azure Blob Storage - PowerShell ####################  
  2.  
  3. ## Input Parameters  
  4. $resourceGroupName="azpractice"  
  5. $storageAccName="azstorageacc1122020"  
  6. $storageContainerName="container001"  
  7. $fileName="testdoc"  
  8. $filePath=".\testdoc.docx"   
  9.  
  10. ## Connect to Azure Account  
  11. Connect-AzAccount   
  12.  
  13. ## Function to upload blob contents  
  14. Function UploadBlobContent  
  15. {  
  16.     Write-Host -ForegroundColor Green "Uploading blob content.."  
  17.     ## Get the storage account  
  18.     $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName      
  19.     ## Get the storage account context  
  20.     $ctx=$storageAcc.Context    
  21.     ## Upload a file  
  22.     Set-AzStorageBlobContent -Container $storageContainerName -File $filePath -Blob $fileName -Context $ctx -Force      
  23. }   
  24.   
  25. UploadBlobContent   
  26.  
  27. ## Disconnect from Azure Account  
  28. Disconnect-AzAccount  
Open the Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command.

.\script.ps1

Result:

Blob contents uploaded successfully. 

Reference:

https://docs.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-3.3.0

Summary:

In this blog, you saw how to upload blob contents in Azure Storage Account using PowerShell.