Powershell script to list out the file share names and their total usage in all storage account of type standard

Shashikanth M S 40 Reputation points
2024-08-07T12:37:27.7066667+00:00

i have PowerShell script which list all file share names and their total quota limit for all standard type storage accounts in every resource group.

but i don't find any script that retrieves usage of each file shares.

is there any script to list all file shares with their total usage?

for example: fileshare "FS1" of standard type storage account "ST1" in resource group "RG1" total usage is "500MB" out of total quota "1gb", fileshare "FS2" of standard type storage account "ST1" in resource group "RG1" total usage is "600MB" out of total quota "1gb"

fileshare "FS3" of standard type storage account "ST2" in resource group "RG2" total usage is "800MB" out of total quota "1gb"

fileshare "FS4" of standard type storage account "ST2" in resource group "RG2" total usage is "900MB" out of total quota "1gb"

so that script should print all file share names, total quota and total usage

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,534 questions
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Nehruji R 8,181 Reputation points Microsoft External Staff Moderator
    2024-08-09T09:13:06.9333333+00:00

    Shashikanth M S, I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!

    Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer. Accepted answers show up at the top, resulting in improved discoverability for others.

    Issue: Customer would like to find a script that retrieves usage of each file shares in azure storage and to list all file shares with their total usage.

    Solution:  Using the below script, customer was able to find the usage details of Azure file share.

    Load the Azure PowerShell module
    Import-Module Az
    
    Function to get file share usage information
    function Get-FileShareUsage {
    
    param(
    
    [Parameter(Mandatory=$true)]
    
    [string]$ResourceGroupName,
    
    [Parameter(Mandatory=$true)]
    
    [string]$StorageAccountName,
    
    [Parameter(Mandatory=$true)]
    
    [string]$ShareName
    
    )
    
    try {
    
    $share = Get-AzRmStorageShare -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -Name $ShareName -GetShareUsage
    
    $shareUsageGB = [math]::Round($share.ShareUsageBytes / 1GB, 2)
    
    $share | Select-Object ResourceGroupName, StorageAccountName, Name, QuotaGiB, EnabledProtocols, AccessTier, Deleted, Version, @{Name="ShareUsageGB"; Expression={$shareUsageGB}}
    
    } catch {
    
    Write-Warning "Error retrieving file share usage: $($_.Exception.Message)"
    
    $null
    
    }
    
    }
    
    Initialize an array to hold the results
    $results = @()
    
    Get all resource groups
    $resourceGroups = Get-AzResourceGroup
    
    foreach ($resourceGroup in $resourceGroups) {
    
    Get storage accounts in the resource group
    $storageAccounts = Get-AzStorageAccount -ResourceGroupName $resourceGroup.ResourceGroupName
    
    foreach ($storageAccount in $storageAccounts) {
    
    Get file shares for the storage account
    $fileShares = Get-AzRmStorageShare -ResourceGroupName $resourceGroup.ResourceGroupName -StorageAccountName $storageAccount.StorageAccountName
    
    foreach ($fileShare in $fileShares) {
    
    $fileShareUsage = Get-FileShareUsage -ResourceGroupName $resourceGroup.ResourceGroupName -StorageAccountName $storageAccount.StorageAccountName -ShareName $fileShare.Name
    
    if ($fileShareUsage) {
    
    Add the usage information to the results array
    $results += $fileShareUsage
    
    }
    
    }
    
    }
    
    }
    
    Format the results for console display
    $results | Format-Table -AutoSize
    
    Export the results to a CSV file
    $results | Export-Csv -Path "FileShareUsage.csv" -NoTypeInformation
    
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2024-08-07T15:38:20.7733333+00:00
    0 comments No comments

  2. Shashikanth M S 40 Reputation points
    2024-08-07T15:42:38.4633333+00:00

    Below script will give expected output

    Load the Azure PowerShell module

    Import-Module Az

    Function to get file share usage information

    function Get-FileShareUsage {

    param(

    [Parameter(Mandatory=$true)]

    [string]$ResourceGroupName,

    [Parameter(Mandatory=$true)]

    [string]$StorageAccountName,

    [Parameter(Mandatory=$true)]

    [string]$ShareName

    )

    try {

    $share = Get-AzRmStorageShare -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -Name $ShareName -GetShareUsage

    $shareUsageGB = [math]::Round($share.ShareUsageBytes / 1GB, 2)

    $share | Select-Object ResourceGroupName, StorageAccountName, Name, QuotaGiB, EnabledProtocols, AccessTier, Deleted, Version, @{Name="ShareUsageGB"; Expression={$shareUsageGB}}

    } catch {

    Write-Warning "Error retrieving file share usage: $($_.Exception.Message)"

    $null

    }

    }

    Initialize an array to hold the results

    $results = @()

    Get all resource groups

    $resourceGroups = Get-AzResourceGroup

    foreach ($resourceGroup in $resourceGroups) {

    Get storage accounts in the resource group

    $storageAccounts = Get-AzStorageAccount -ResourceGroupName $resourceGroup.ResourceGroupName

    foreach ($storageAccount in $storageAccounts) {

    Get file shares for the storage account

    $fileShares = Get-AzRmStorageShare -ResourceGroupName $resourceGroup.ResourceGroupName -StorageAccountName $storageAccount.StorageAccountName

    foreach ($fileShare in $fileShares) {

    $fileShareUsage = Get-FileShareUsage -ResourceGroupName $resourceGroup.ResourceGroupName -StorageAccountName $storageAccount.StorageAccountName -ShareName $fileShare.Name

    if ($fileShareUsage) {

    Add the usage information to the results array

    $results += $fileShareUsage

    }

    }

    }

    }

    Format the results for console display

    $results | Format-Table -AutoSize

    Export the results to a CSV file

    $results | Export-Csv -Path "FileShareUsage.csv" -NoTypeInformation

    0 comments No comments

  3. Nehruji R 8,181 Reputation points Microsoft External Staff Moderator
    2024-08-08T13:53:57.4866667+00:00

    Hello Shashikanth M S,

    Greetings! Welcome to Microsoft Q&A Platform.

    Try with the below example PowerShell script to that will list all file shares in the format you specified:

    # Login to Azure
    Connect-AzAccount
    
    # Get all storage accounts
    $storageAccounts = Get-AzStorageAccount
    
    # Iterate over each storage account
    foreach ($storageAccount in $storageAccounts) {
        # Get the context of the storage account
        $context = $storageAccount.Context
    
        # Get all file shares in the storage account
        $fileShares = Get-AzStorageShare -Context $context
    
        # Iterate over each file share
        foreach ($fileShare in $fileShares) {
            # Get the quota and usage of the file share
            $quota = $fileShare.ShareQuota
            $usage = ($fileShare | Get-AzStorageFile -Context $context).Length
    
            # Convert usage to MB
            $usageMB = [math]::Round($usage / 1MB, 2)
    
            # Print the file share name, storage account name, resource group, quota, and usage
            Write-Output "File share '$($fileShare.Name)' of standard type storage account '$($storageAccount.StorageAccountName)' in resource group '$($storageAccount.ResourceGroupName)' total usage is '$($usageMB)MB' out of total quota '$($quota)GB'"
        }
    }
    
    

    Note: This is Sample script and please modify as per your requirements and structure.

    Similar thread for reference - https://stackoverflow.com/questions/55728404/fast-way-to-check-azure-storage-account-file-share-used-capacity-with-powershell,https://learn.microsoft.com/en-us/answers/questions/1179319/how-can-i-list-all-fileshares-and-blobs-for-all-st,https://learn.microsoft.com/en-us/answers/questions/1428061/find-files-or-directory-using-powershell-in-both-f, https://learn.microsoft.com/en-us/answers/questions/1565053/azure-powershell-script-for-listing-azure-file-sha,

    Hope this answer helps! please let us know if you have any further queries. I’m happy to assist you further.


    Please "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

  4. Shashikanth M S 40 Reputation points
    2024-08-08T17:23:59.35+00:00

    Nehruji R the script you are shared is from any AI tools like chatgpt/gemini/copilot? i got answer already and shared in comment that i posted previously.

    the cmdlet Get-AzStorageFile will not give you usage of fileshares, instead

    $share = Get-AzRmStorageShare -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -Name $ShareName -GetShareUsage

    will give you correct output. if you are from MS then i recommend you to test from your side then post it.

    0 comments No comments

Your answer

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