Skip to content

Commit 883bc41

Browse files
vishalk9040sihegde
authored andcommitted
PowerShell Script to Export Used Capacity of Azure Storage Accounts in a Subscription.ps1
1 parent 9816610 commit 883bc41

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#This PowerShell script will help you to export Used Capacity of Azure Storage Accounts in a Subscription.
2+
3+
#Disclaimer
4+
#By using the following materials or sample code you agree to be bound by the license terms below
5+
#and the Microsoft Partner Program Agreement the terms of which are incorporated herein by this reference.
6+
#These license terms are an agreement between Microsoft Corporation (or, if applicable based on where you
7+
#are located, one of its affiliates) and you. Any materials (other than sample code) we provide to you
8+
#are for your internal use only. Any sample code is provided for the purpose of illustration only and is
9+
#not intended to be used in a production environment. We grant you a nonexclusive, royalty-free right to
10+
#use and modify the sample code and to reproduce and distribute the object code form of the sample code,
11+
#provided that you agree: (i) to not use Microsoft’s name, logo, or trademarks to market your software product
12+
#in which the sample code is embedded; (ii) to include a valid copyright notice on your software product in
13+
#which the sample code is embedded; (iii) to provide on behalf of and for the benefit of your subcontractors
14+
#a disclaimer of warranties, exclusion of liability for indirect and consequential damages and a reasonable
15+
#limitation of liability; and (iv) to indemnify, hold harmless, and defend Microsoft, its affiliates and
16+
#suppliers from and against any third party claims or lawsuits, including attorneys’ fees, that arise or result
17+
#from the use or distribution of the sample code."
18+
19+
#Used Capacity in MB
20+
21+
# Login to Azure
22+
Connect-AzAccount
23+
24+
# Set your subscription (replace with your SubscriptionId or Name)
25+
$subscriptionId = "<SubscriptionId>" # or use Subscription Name
26+
27+
# Set the subscription context
28+
Set-AzContext -SubscriptionId $subscriptionId | Out-Null
29+
$currentSub = (Get-AzSubscription -SubscriptionId $subscriptionId).Name
30+
31+
# Prepare output array
32+
$results = @()
33+
34+
# Get all resource groups in the subscription
35+
$resourceGroups = Get-AzResourceGroup
36+
37+
foreach ($rg in $resourceGroups) {
38+
$currentRG = $rg.ResourceGroupName
39+
40+
# Get all storage accounts in the resource group
41+
$storageAccounts = Get-AzStorageAccount -ResourceGroupName $currentRG
42+
43+
foreach ($sa in $storageAccounts) {
44+
$storageAccount = $sa.StorageAccountName
45+
$CurrentSAID = $sa.Id
46+
47+
# Get UsedCapacity metric (latest datapoint)
48+
$metric = Get-AzMetric -ResourceId $CurrentSAID -MetricName "UsedCapacity"
49+
50+
$latest = $metric.Data | Sort-Object TimeStamp -Descending | Select-Object -First 1
51+
52+
if ($latest.Total -ne $null) {
53+
$usedCapacityMB = ($latest.Total / 1MB) #
54+
} elseif ($latest.Average -ne $null) {
55+
$usedCapacityMB = ($latest.Average / 1MB) #
56+
} else {
57+
$usedCapacityMB = 0
58+
}
59+
60+
# Add result to array
61+
$results += [pscustomobject]@{
62+
Subscription = $currentSub
63+
ResourceGroup = $currentRG
64+
StorageAccount = $storageAccount
65+
UsedCapacityMB = $usedCapacityMB #
66+
}
67+
}
68+
}
69+
70+
# Export results to CSV
71+
$results | Export-Csv -Path ".\StorageAccountsUsedCapacity.csv" -NoTypeInformation
72+
Write-Host "Export completed: .\StorageAccountsUsedCapacity.csv"
73+
74+
75+
#Used Capacity in GB
76+
77+
# Login to Azure
78+
Connect-AzAccount
79+
80+
# Set your subscription (replace with your SubscriptionId or Name)
81+
$subscriptionId = "<SubscriptionId>" # or use Subscription Name
82+
83+
# Set the subscription context
84+
Set-AzContext -SubscriptionId $subscriptionId | Out-Null
85+
$currentSub = (Get-AzSubscription -SubscriptionId $subscriptionId).Name
86+
87+
# Prepare output array
88+
$results = @()
89+
90+
# Get all resource groups in the subscription
91+
$resourceGroups = Get-AzResourceGroup
92+
93+
foreach ($rg in $resourceGroups) {
94+
$currentRG = $rg.ResourceGroupName
95+
96+
# Get all storage accounts in the resource group
97+
$storageAccounts = Get-AzStorageAccount -ResourceGroupName $currentRG
98+
99+
foreach ($sa in $storageAccounts) {
100+
$storageAccount = $sa.StorageAccountName
101+
$CurrentSAID = $sa.Id
102+
103+
# Get UsedCapacity metric (latest datapoint)
104+
$metric = Get-AzMetric -ResourceId $CurrentSAID -MetricName "UsedCapacity"
105+
106+
$latest = $metric.Data | Sort-Object TimeStamp -Descending | Select-Object -First 1
107+
108+
if ($latest.Total -ne $null) {
109+
$usedCapacityGB = ($latest.Total / 1GB) # ✅ no rounding
110+
} elseif ($latest.Average -ne $null) {
111+
$usedCapacityGB = ($latest.Average / 1GB) #
112+
} else {
113+
$usedCapacityGB = 0
114+
}
115+
116+
# Add result to array
117+
$results += [pscustomobject]@{
118+
Subscription = $currentSub
119+
ResourceGroup = $currentRG
120+
StorageAccount = $storageAccount
121+
UsedCapacityGB = $usedCapacityGB #
122+
}
123+
}
124+
}
125+
126+
# Export results to CSV
127+
$results | Export-Csv -Path ".\StorageAccountsUsedCapacity.csv" -NoTypeInformation
128+
Write-Host "Export completed: .\StorageAccountsUsedCapacity.csv"
129+
130+

0 commit comments

Comments
 (0)