skip to Main Content

Trying to get the list of unused/inactive storage accounts in azure using powershell. Below is my script which im trying it will provide the storage account name and last modified date of your Azure storage accounts, but i need to list only the unused storage accounts names not all the storage accounts, for that some condition/filter i need to provide to achieve the same. Please assist me to solve this. Thanks in Advance

It will output the results into a table detailing the name and last modified date of your Azure storage accounts.

& {
foreach ($storageAccount in Get-AzStorageAccount) {
$storageAccountName = $storageAccount.StorageAccountName
$resourceGroupName = $storageAccount.ResourceGroupName


  # Get storage account key
     $storageAccountKey = (Get-AzStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
    
     # Create storage account context using above key
     $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    
     # Get the last modified date
     $lastModified = Get-AzStorageContainer -Context $context | Sort-Object -Property @{Expression = {$_.LastModified.DateTime}} | Select-Object -Last 1 -ExpandProperty LastModified

             # Collect the information to output to a table when the for loop has completed
             New-Object psobject -Property @{
                 Name = $storageAccountName;
                 LastModified = $lastModified.DateTime;
                 ResourceGroupName = $resourceGroupName
             }

 }
} | Format-Table Name, LastModified, ResourceGroupName -autosize

2

Answers


  1. enter image description hereUse get-date
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date

    and use the Where-Object.

         # ADD THIS
     $lastModDate = (get-date).AddDays(-5).Date
     $lastMod = $lastModified | Where-Object { ($_.DateTime).Date -lt $lastModDate}
     
     # If $lastMod.DateTime is NOT empty, then:
     if ($lastMod.DateTime) { 
     
        # Write-Host "variable is NOT null " + $storageAccountName # For testing purpose
        # Collect the information to output to a table when the for loop has completed
        New-Object psobject -Property @{
            Name = $storageAccountName;
            LastModified = $lastMod.DateTime; # CHANGE THIS
            ResourceGroupName = $resourceGroupName
        }
     }
    

    https://www.techielass.com/find-unused-storage-accounts-in-azure/

    With your script:
    enter image description here

    With my changes:
    enter image description here

    Login or Signup to reply.
  2. I tried to reproduce the same in my environment and got the same result as below:

    By using the same script, I got the storage account name and last modified date of the Azure storage accounts.

    enter image description here

    To get only the unused/inactive storage accounts in azure using PowerShell, I modified the script like below:

    I agree with @Niclas, you need make use of get-date command.

    & {
      foreach ($storageAccount in Get-AzStorageAccount) {
        $storageAccountName = $storageAccount.StorageAccountName
        $resourceGroupName = $storageAccount.ResourceGroupName
        $storageAccountKey = (Get-AzStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
        $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
        $lastModified = Get-AzStorageContainer -Context $context | Sort-Object -Property @{Expression = {$_.LastModified.DateTime}} | Select-Object -Last 1 -ExpandProperty LastModified
        $unusedacc = (Get-Date).AddDays(-10)
        if ($lastModified.DateTime -lt $unusedacc) {
            New-Object psobject -Property @{
            Name = $storageAccountName;
            LastModified = $lastModified.DateTime;
            ResourceGroupName = $resourceGroupName
          }
        }
      }
    } | Format-Table Name, LastModified, ResourceGroupName -autosize
    

    enter image description here

    Note: Based on your requirement you can change the number of days in this line $unusedacc = (Get-Date).AddDays(-10).

    If there are no unused Storage accounts, then it will return blank results like below:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search