In this blog, you will see how to list directories and files from Azure File Share using PowerShell.
Prerequisites
PowerShell Script
Open Notepad and paste the following script. Save the file as script.ps1.
- ################# Azure Blob Storage - PowerShell ####################
-
- ## Input Parameters
- $resourceGroupName="azpractice"
- $storageAccName="azstorageacc1122020"
- $fileShareName="azevents2020"
- $directoryPath="Presentation"
-
- ## Connect to Azure Account
- Connect-AzAccount
-
- ## Function to Lists directories and files
- Function GetFiles
- {
- Write-Host -ForegroundColor Green "Lists directories and files.."
- ## Get the storage account context
- $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
- ## List directories
- $directories=Get-AZStorageFile -Context $ctx -ShareName $fileShareName
- ## Loop through directories
- foreach($directory in $directories)
- {
- write-host -ForegroundColor Magenta " Directory Name: " $directory.Name
- $files=Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Path $directory.Name | Get-AZStorageFile
- ## Loop through all files and display
- foreach ($file in $files)
- {
- write-host -ForegroundColor Yellow $file.Name
- }
- }
- }
-
- GetFiles
-
- ## Disconnect from Azure Account
- 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.