In this blog, you will see how to list directories and files from 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. $directoryPath="Presentation"
  7. ## Connect to Azure Account
  8. Connect-AzAccount
  9. ## Function to Lists directories and files
  10. Function GetFiles
  11. {
  12. Write-Host -ForegroundColor Green "Lists directories and files.."
  13. ## Get the storage account context
  14. $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
  15. ## List directories
  16. $directories=Get-AZStorageFile -Context $ctx -ShareName $fileShareName
  17. ## Loop through directories
  18. foreach($directory in $directories)
  19. {
  20. write-host -ForegroundColor Magenta " Directory Name: " $directory.Name
  21. $files=Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Path $directory.Name | Get-AZStorageFile
  22. ## Loop through all files and display
  23. foreach ($file in $files)
  24. {
  25. write-host -ForegroundColor Yellow $file.Name
  26. }
  27. }
  28. }
  29. GetFiles
  30. ## Disconnect from Azure Account
  31. Disconnect-AzAccount
Open 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/get-azstoragefile?view=azps-3.3.0
Summary
Thus, in this blog, you saw how to list directories and files from Azure File Share using PowerShell.