How to Upload Files to Azure File Share using PowerShell

Introduction 

In this blog, you will see how to upload files to Azure File Share 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. $fileShareName="azevents2020"  
  7. $fileName="testdoc.docx"  
  8. $folderPath="/"  
  9.  
  10. ## Connect to Azure Account  
  11. Connect-AzAccount   
  12.  
  13. ## Function upload files to file share  
  14. Function UploadFiles  
  15. {  
  16.     Write-Host -ForegroundColor Green "Upload files to file share.."    
  17.     ## Get the storage account context  
  18.     $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context  
  19.     ## Get the file share  
  20.     $fileShare=Get-AZStorageShare -Context $ctx -Name $fileShareName  
  21.     ## Upload the file  
  22.     Set-AzStorageFileContent -Share $fileShare -Source $fileName -Path $folderPath -Force  
  23. }  
  24.   
  25. UploadFiles   
  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

 

Reference

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

Summary

In this blog, you saw how to upload files to Azure File Share using PowerShell.