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