Hi @Rakesh Kumar
Welcome to Microsoft Q&A, thanks for posting your query.
PowerShell code provides a comprehensive solution to retrieve all requested details for Azure Storage Accounts, including the last used date, creation date, created by, current size, subscription name and ID, and resource group.
Check the below steps:
List of Azure storage accounts in the PowerShell.
Get-AzStorageAccount
Retrieves all storage accounts in the selected Azure subscription.
Last Used Date:
The code uses the Invoke-RestMethod
with a REST API request to retrieve the most recent blob activity:
# Get the last used date of the storage account
$lastUsedTime = $storageAccount.PrimaryEndpoints.Blob.ToString() + "?comp=list&restype=container&maxresults=1"
$lastUsedTime = Invoke-RestMethod -Uri $lastUsedTime -Method GET -Headers $storageAccount.Context.DefaultRequestHeaders -UseBasicParsing
$lastUsedTime = $lastUsedTime.EnumerationResults.Containers.ContainerProperties.LastModified
Creation Date:
You can use the Get-AzStorageAccount command. This command retrieves the properties of the storage account, including the creation date.
$storageAccount = Get-AzStorageAccount -ResourceGroupName <resource-group> -Name <storage-account>
$creationDate = $storageAccount.CreationTime
Storage Account Created by:
# Get the created by user of the storage account
$createdBy = $storageAccount.Tags.CreatedBy
Accesses the CreatedBy
tag if it exists. This tag must be manually added or configured by policy.
The Current Size of the Storage Account:
Uses the REST API to calculate the total capacity:
$currentSize = $storageAccount.PrimaryEndpoints.Blob.ToString() + "?restype=account&comp=properties"
$currentSize = Invoke-RestMethod -Uri $currentSize -Method GET -Headers $storageAccount.Context.DefaultRequestHeaders -UseBasicParsing
$currentSize = $currentSize.Properties.TotalCapacity / 1GB
Subscription name and ID:
Retrieves the subscription name and ID associated with the storage account:
$subscriptionName = (Get-AzSubscription -SubscriptionId $storageAccount.Id.Split('/')[2]).Name
$subscriptionId = $storageAccount.Id.Split('/')[2]
Resource Group of the Storage Account:
Accesses the ResourceGroupName
property directly.
# Get the resource group of the storage account
$resourceGroup = $storageAccount.ResourceGroupName
Combining all sections, you could retrieve all details in one script:
Connect-AzAccount
# Retrieve all storage accounts
$storageAccounts = Get-AzStorageAccount
foreach ($storageAccount in $storageAccounts) {
$lastUsedTime = $storageAccount.PrimaryEndpoints.Blob.ToString() + "?comp=list&restype=container&maxresults=1"
$lastUsedTime = Invoke-RestMethod -Uri $lastUsedTime -Method GET -Headers $storageAccount.Context.DefaultRequestHeaders -UseBasicParsing
$lastUsedDate = $lastUsedTime.EnumerationResults.Containers.ContainerProperties.LastModified
$createdDate = $storageAccount.CreationTime
$createdBy = $storageAccount.Tags.CreatedBy
$currentSize = $storageAccount.PrimaryEndpoints.Blob.ToString() + "?restype=account&comp=properties"
$currentSize = Invoke-RestMethod -Uri $currentSize -Method GET -Headers $storageAccount.Context.DefaultRequestHeaders -UseBasicParsing
$currentSizeGB = $currentSize.Properties.TotalCapacity / 1GB
$subscriptionName = (Get-AzSubscription -SubscriptionId $storageAccount.Id.Split('/')[2]).Name
$subscriptionId = $storageAccount.Id.Split('/')[2]
$resourceGroup = $storageAccount.ResourceGroupName
# Display or store the details
Write-Output "Storage Account: $($storageAccount.StorageAccountName)"
Write-Output "Last Used Date: $lastUsedDate"
Write-Output "Created Date: $createdDate"
Write-Output "Created By: $createdBy"
Write-Output "Current Size (GB): $currentSizeGB"
Write-Output "Subscription Name: $subscriptionName"
Write-Output "Subscription ID: $subscriptionId"
Write-Output "Resource Group: $resourceGroup"
Write-Output "---------------------------"
}
Please let us know if you have any further queries. I’m happy to assist you further.
Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.