Know Your Azure VM Created Date Using PowerShell & AZCli

Do you want know when was your Azure VM created? Then this script will retrieve date time created for the given Azure VM(s). Not sure whether there is direct way to retrieve the VM created date, but I couldn’t find any CmdLet to know the VM created date.

However, I have written a PowerShell function to know the VM created date by considering the VM OS disk creation date as VM creation date.

The function accepts the combination of Resource Group Name and VM Name as mandatory parameters or VM object(s), and you will see the output as below…

And even I have tried the same using Azure Cli as well, and you get the output like this…

And, here’s the script…

This script pulls the date and the time on which the Azure VM(s) created.
This script accepts Resource Group & VM Name as mandatory parameters and accepts VM object(s) optionally.
Since there is no direct Cmdlet to fetch the create date, it is considered the disk create date as VM create date.
Function Get-AzVMCreateDate
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [string] $ResourceGroupName,                                        # Resource Group Name
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [string] $Name,                                                     # VM Name
        [parameter(Mandatory=$false, ValueFromPipeline=$true)]
        [System.Object[]] $VMObject                                         # VM Object
        # Check if the VM Object is from the pipeline
        $IsItVMObject = $null -ne $VMObject

        # Checking login, if not asking for the login
        if (($IsItVMObject -eq $false) -and ($null -eq $(Get-AzContext -ErrorAction SilentlyContinue)))

        # Output array object
        $VMArray = @()

        # Fetching the VM details from Resource Group Name and VM Name if provided
        if ($IsItVMObject -eq $false)
            $VMObject = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $Name
        foreach ($VM in $VMObject)
            # Get the OS Disk Name
            $VMDiskName = $VM.StorageProfile.OsDisk.Name

            # Get the Disk Info
            $VMDiskInfo = Get-AzDisk -ResourceGroupName $VM.ResourceGroupName -DiskName $VMDiskName

            # Get disk create date & time
            $VMCreatedDate = $VMDiskInfo.TimeCreated

            # Add result to the output array
            $VMArray += New-Object -TypeName psobject -Property @{
                ResourceGroup = $VM.ResourceGroupName
                VMName = $VM.Name
                CreateDate = $VMCreatedDate

        # Output
        return ($VMArray | Select-Object ResourceGroup, VMName, CreateDate)

<# Load the function
    PS /> . ./Get-AzVMCreateDate.ps1    # on Linux
    PS \> . .\Get-AzVMCreateDate.ps1    # on Windows

<# Calling the function
    PS > Get-AzVMCreateDate
# Login to your Azure Subscription
#az login 

# Declare variables

# Get VM OS disk name
vmdiskname=$(az vm show --resource-group $resourceGroupName --name $vmName -d --query "" -o tsv)

# Get VM OS disk create date
createDate=$(az disk show --resource-group LINUX-RG --name $vmdiskname --query "timeCreated" -o tsv)

# Convert the date to readable format
createDatef=$(date -d $createDate '+%Y/%m/%d %T')

# Output
printf "%-20s | %-20s | %-20s\n" "$resourceGroupName" "$vmName" "$createDatef"

Share it on     |   |   |   | 

Get Windows Activation Status Across The Plant


Convert Object Properties Into HashTable

comments powered by Disqus