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. ## Input Parameters
  3. $resourceGroupName="azpractice"
  4. $storageAccName="azstorageacc1122020"
  5. $fileShareName="azevents2020"
  6. $fileName="testdoc.docx"
  7. $folderPath="/"
  8. ## Connect to Azure Account
  9. Connect-AzAccount
  10. ## Function upload files to file share
  11. Function UploadFiles
  12. {
  13. Write-Host -ForegroundColor Green "Upload files to file share.."
  14. ## Get the storage account context
  15. $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
  16. ## Get the file share
  17. $fileShare=Get-AZStorageShare -Context $ctx -Name $fileShareName
  18. ## Upload the file
  19. Set-AzStorageFileContent -Share $fileShare -Source $fileName -Path $folderPath -Force
  20. }
  21. UploadFiles
  22. ## Disconnect from Azure Account
  23. 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.