Powershell script for Azure storage accounts

Rakesh Kumar 461 Reputation points
2024-11-13T08:46:01.9566667+00:00

Hi All,

can anyone help me to get the below details for azure storage account using powershell.

list of Azure Storage Accounts

Last used date

Created date

Created by

Current size

Subscription name

Subscription id

Resource group

Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,271 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vinod Kumar Reddy Chilupuri 1,600 Reputation points Microsoft Vendor
    2024-11-13T13:25:23.07+00:00

    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. 

    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.