Home > Enterprise >  Find/list the Unused storage accounts in azure using powershell
Find/list the Unused storage accounts in azure using powershell

Time:02-02

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

CodePudding user response:

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

CodePudding user response:

enter image description hereUse get-date
enter image description here

With my changes:
enter image description here

  • Related