Share via

How do I get all Azure Storage Fileshare folders/subfolders and files via Powershell

NJ 25 Reputation points
2025-07-09T11:45:24.9666667+00:00

Hi,

I am new to PowerShell and the Azstorage commands.

I am trying to get a list of all files and folders in an Azure Storage fileshare but having difficulty getting my script to work. I have tried various combinations but it is not finding the subfolders or the files.

I have a test fileshare which has 2 folders at the root level Dir1 and Dir 2 and 1 subfolder and 1 file

Dir1

Subdir1

TestFile.PDF

Dir2

The script i am trying at the moment is below. I am initially trying to set the 1st folder DIr1 as the input to the function with the idea that it then pulls in the subfolder, etc but it does not work as it is not bringing back the subfolder under Dir1 and I am not sure why, any help would be appreciated.

Variables

$SasToken = "xxxxxxxxxxxxxx"

$fileShareName ="yyyyyyyyyy"

Create a storage context

$StorageContext = New-AzStorageContext -StorageAccountName "zzzzzzzzzz" -SasToken $SasToken

$directories=Get-AzStorageFile -ShareName $fileshareName -Context $storageContext | Where-Object {$_.GetType().Name -eq "AzureStorageFileDirectory"}

foreach ($item in $directories) {

Write-Output "Folder is : $($item.Name)"

$directoryPath = $item.Name

Get-AzFileShareContent -shareName $fileShareName -directoryPath $directoryPath

}

Function to recursively list files and folders

function Get-AzFileShareContent {

param (

    [string]$fileshareName,

    [string]$directoryPath #= ""

)

# List files and directories in the current directory

$files = Get-AzStorageFile -Context $storageContext -ShareName $fileshareName -Path $directoryPath | Get-AzStorageFile

foreach ($file in $files) {

    if ($file.GetType().Name -eq "AzureStorageFileDirectory") {

        # Output the folder path

        Write-Output "Folder: $($directoryPath)\$($file.Name)"

        # Recursively call the function for subdirectories

        Get-AzFileShareContent -shareName $fileshareName -directoryPath "$directoryPath\$($file.Name)"

    } else {

        # Output the file path

        Write-Output "File: $($directoryPath)\$($file.Name)"

    }

}

}

Azure Storage
Azure Storage

Globally unique resources that provide access to data management services and serve as the parent namespace for the services.

0 comments No comments

Answer accepted by question author

Muskan Tomar 565 Reputation points Microsoft Employee
2025-07-09T12:12:38.9266667+00:00

Hi NJ,

you are very close! The main issue is with this line in your recursive function:

$files = Get-AzStorageFile -Context $storageContext -ShareName $fileshareName -Path $directoryPath | Get-AzStorageFile

Remove the Pipe from the recursive function. The second Get-AzStorageFile is unnecessary and causes the function to fail in retrieving subfolders and files.

Apart from that few basic changes you can try

1.Start from the root path ("") to ensure all folders and files are included.


$directories = Get-AzStorageFile -ShareName $fileshareName -Context $storageContext | Where-Object {$_.GetType().Name -eq "AzureStorageFileDirectory"}

doesn’t specify a -Path, so it defaults to the root and only lists the root-level directories. It doesn’t trigger recursion into subdirectories unless explicitly handled.

  1. Function call placement

The recursive function is defined after it’s called in the loop:

foreach ($item in $directories) {
    ...
    Get-AzFileShareContent -shareName $fileShareName -directoryPath $directoryPath
}

define the function before calling to avoid scope issues

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.