Powershell script to list all resources/VMs in azure subscription for which decommissioning date will be in next few days .

Bhavishka Sathawane 26 Reputation points
2022-08-25T17:57:22.157+00:00

I'm trying to get value of decommission tag for all resources in my subscriptions.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,410 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Andreas Baumgarten 100.8K Reputation points MVP
    2022-08-25T20:15:19.227+00:00

    Hi @Bhavishka Sathawane ,

    maybe these 2 scripts are helpful to get started.

    Get Azure VMs:

    $vm = Get-AzVM  
    $vm | ForEach-Object {  
        if ($($_.Tags.decommission)) {  
            $DecommissionDate = $_.Tags.decommission  
            Write-Output "DecomissionDate for $($_.Name) = $DecommissionDate"  
        }  
        else { Write-Output "DecomissionDate for $($_.Name) is unknown" }  
    }  
    

    Get All Azure Resources:

    $resources = Get-AzResource  
    $resources | ForEach-Object {  
        if ($($_.Tags.decommission)) {  
            $DecommisssionDate = $_.Tags.decommission  
            Write-Output "DecomissionDate for $($_.Name) = $DecommissionDate"  
        }  
        else { Write-Output "DecomissionDate for $($_.Name) is unknown" }  
    }  
    

    The variable `$DecommissionDate contains the value as a string.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments