I need a Powershell script to get the Azure File Storage Total and Free usage in percentage.
Loading
I need a Powershell script to get the Azure File Storage Total and Free usage in percentage.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jul 18, 2023, 11:58 PM EDT
Try below:
# Install the AzureRM module if not already installed # Install-Module -Name AzureRM # Connect to Azure using your credentials Connect-AzAccount # Set the Azure subscription context Set-AzContext -SubscriptionId "your_subscription_id" # Provide the name of your Azure File Storage account $storageAccountName = "your_storage_account_name" $resourceGroupName = "your_resource_group_name" # Get the storage account $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName # Get the usage metrics for the storage account $usageMetrics = Get-AzMetric -ResourceId $storageAccount.Id -TimeGrain "PT1H" -MetricName "UsedCapacity" -StartTime (Get-Date).AddHours(-1) -EndTime (Get-Date) | Sort-Object Timestamp -Descending | Select-Object -First 1 # Get the total and free capacity $totalCapacity = $usageMetrics.Maximum $freeCapacity = $usageMetrics.Maximum - $usageMetrics.Average # Calculate the usage percentage $usagePercentage = ($usageMetrics.Average / $usageMetrics.Maximum) * 100 # Output the results Write-Output "Total Capacity: $totalCapacity" Write-Output "Free Capacity: $freeCapacity" Write-Output "Usage Percentage: $usagePercentage%"Thanks
Viknaraj ManogararajahRank #35 · 36.6K reputationPosted Jul 19, 2023, 8:08 AM EDT
Thank you, Naimish. I appreciate your active response! It works for me.
Viknaraj ManogararajahRank #35 · 36.6K reputationPosted Jul 24, 2023, 12:28 PM EDT
Hi Naimish,
I'm getting the output as 0. Do I need to add anything?
Viknaraj ManogararajahRank #35 · 36.6K reputationPosted Jul 24, 2023, 12:51 PM EDT
Now I'm getting "Get-AzMetric: Cannot bind parameter TimeGrain. Cannot convert value PT1H to type System.TimeSpan. Error: String PT1H was not recognized as a valid TimeSpan."
Saravanan GanesanRank #153 · 15.1K reputationPosted Aug 4, 2023, 4:26 PM EDT
# Connect to Azure
Connect-AzAccount
# Replace and with your actual resource group and storage account names""
$resourceGroupName = "
$storageAccountName = "
# Get the storage account
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
# Get the total storage capacity
$totalCapacity = $storageAccount.PrimaryEndpoints.File + "default" | Get-AzStorageUsage -IncludeStatistics | Where-Object { $_.Name -eq "FileCapacity" } | Select-Object -ExpandProperty Maximum
# Get the used storage capacity
$usedCapacity = $storageAccount.PrimaryEndpoints.File + "default" | Get-AzStorageUsage -IncludeStatistics | Where-Object { $_.Name -eq "FileCapacity" } | Select-Object -ExpandProperty CurrentValue
# Calculate the free capacity and percentage
$freeCapacity = $totalCapacity - $usedCapacity
$freePercentage = ($freeCapacity / $totalCapacity) * 100
$usedPercentage = 100 - $freePercentage
# Output the results
Write-Output "Total Storage Capacity: $totalCapacity bytes"
Write-Output "Used Storage Capacity: $usedCapacity bytes"
Write-Output "Free Storage Capacity: $freeCapacity bytes"
Write-Output "Used Storage Percentage: $usedPercentage%"
Write-Output "Free Storage Percentage: $freePercentage%"
Replace
andwith your actual resource group and storage account names. The script connects to your Azure account, retrieves the total and used storage capacities for the Azure File Storage, and calculates the free and used percentages. The results are then displayed in the console.Viknaraj ManogararajahRank #35 · 36.6K reputationPosted Aug 14, 2023, 6:12 AM EDT
Received the below error
Get-AzStorageUsage : A parameter cannot be found that matches parameter name 'IncludeStatistics'.
At line:1 char:89
+ ... ints.File + "default" | Get-AzStorageUsage -IncludeStatistics | Where ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzStorageUsage], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.Management.Storage.StorageAccount.GetAzureStorageUsageCommand
Tasadduq BurneyRank #1,427 · 1.1K reputationPosted Aug 17, 2023, 4:08 AM EDT
# Install the Az module if not already installed # Install-Module -Name Az -Force # Authenticate to Azure (you might need to log in) # Connect-AzAccount $storageAccountName = "your_storage_account_name" $resourceGroupName = "your_resource_group_name" $shareName = "your_share_name" $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName $share = Get-AzStorageShare -Context $storageAccount.Context -Name $shareName $totalCapacity = $share.UsageBytes $freeCapacity = $share.Quota - $totalCapacity $totalCapacityGB = $totalCapacity / 1GB $freeCapacityGB = $freeCapacity / 1GB $totalUsagePercentage = ($totalCapacityGB - $freeCapacityGB) / $totalCapacityGB * 100 $freeSpacePercentage = $freeCapacityGB / $totalCapacityGB * 100 Write-Host "Total Usage Percentage: $totalUsagePercentage%" Write-Host "Free Space Percentage: $freeSpacePercentage%"