What is the easiest PowerShell script to check the creation date of Azure VMs?

Rohit Mahajan 20 Reputation points
2024-08-18T11:37:46.3133333+00:00

I'm looking for a simple and effective PowerShell script to retrieve the creation date of Azure virtual machines (VMs). I need a script that can check the creation date for each VM within a resource group or subscription.

I have basic knowledge of PowerShell and Azure, but I'm not sure about the best approach to get this information. Any guidance or example scripts would be greatly appreciated!

Thank you in advance!

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,991 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,552 questions
{count} votes

Accepted answer
  1. akinbade abiola 18,305 Reputation points
    2024-08-18T23:19:27.2033333+00:00

    I found a script here tha does this:

    https://gist.github.com/kpatnayakuni/164e11f4a61be2222e14f4f146fdf93c

    You can review and check

    You can mark it 'Accept Answer' and 'Upvote' if this helped you

    Regards,

    Abiola

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Marcin Policht 25,755 Reputation points MVP
    2024-08-18T11:41:12.3966667+00:00

    Try the following:

    # Set your Azure subscription and resource group name
    $SubscriptionId = "your-subscription-id"
    $ResourceGroupName = "your-resource-group-name"
    # Log in to Azure
    Connect-AzAccount
    # Set the Azure subscription context
    Set-AzContext -SubscriptionId $SubscriptionId
    # Retrieve all VMs in the specified resource group
    $VMs = Get-AzVM -ResourceGroupName $ResourceGroupName
    # Loop through each VM and retrieve the creation date
    foreach ($VM in $VMs) {
        $VMName = $VM.Name
        # Get the creation date from activity log
        $CreationEvent = Get-AzLog -ResourceGroupName $ResourceGroupName `
                                   -ResourceId $VM.Id `
                                   -StartTime (Get-Date).AddYears(-1) `
                                   -MaxRecord 1 `
                                   -DetailedOutput `
                                   | Where-Object { $_.OperationName -eq "Microsoft.Compute/virtualMachines/write" -and $_.Status -eq "Succeeded" } `
                                   | Sort-Object -Property EventTimestamp `
                                   | Select-Object -First 1
        if ($CreationEvent) {
            $CreationDate = $CreationEvent.EventTimestamp
            Write-Output "VM Name: $VMName, Creation Date: $CreationDate"
        } else {
            Write-Output "VM Name: $VMName, Creation Date: Not found"
        }
    }
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth Marcin

    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.