How to list directories and files from Azure File Share using PowerShell

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.  
  3. ## Input Parameters  
  4. $resourceGroupName="azpractice"  
  5. $storageAccName="azstorageacc1122020"  
  6. $fileShareName="azevents2020"  
  7. $directoryPath="Presentation"  
  8.  
  9. ## Connect to Azure Account  
  10. Connect-AzAccount   
  11.  
  12. ## Function to Lists directories and files  
  13. Function GetFiles  
  14. {  
  15.     Write-Host -ForegroundColor Green "Lists directories and files.."    
  16.     ## Get the storage account context  
  17.     $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context  
  18.     ## List directories  
  19.     $directories=Get-AZStorageFile -Context $ctx -ShareName $fileShareName  
  20.     ## Loop through directories  
  21.     foreach($directory in $directories)  
  22.     {  
  23.         write-host -ForegroundColor Magenta " Directory Name: " $directory.Name  
  24.         $files=Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Path $directory.Name | Get-AZStorageFile  
  25.         ## Loop through all files and display  
  26.         foreach ($file in $files)  
  27.         {  
  28.             write-host -ForegroundColor Yellow $file.Name  
  29.         }  
  30.     }  
  31. }  
  32.   
  33. GetFiles   
  34.  
  35. ## Disconnect from Azure Account  
  36. Disconnect-AzAccount   
Open Windows PowerShell window and navigate to the location where the script file was saved.
 
Run the following command.
 
.\script.ps1
 
 
 
Reference
 
 
Summary
 
Thus, in this blog, you saw how to list directories and files from Azure File Share using PowerShell.