Skip to content

Instantly share code, notes, and snippets.

@adbertram
Last active January 2, 2025 16:31
Show Gist options
  • Save adbertram/bc60c82895cd8331c597c9c94f19842e to your computer and use it in GitHub Desktop.
Save adbertram/bc60c82895cd8331c597c9c94f19842e to your computer and use it in GitHub Desktop.
get azure vm creation date
## We can use the OS disk of the VM to see the VM's creation date since it's always created at the same time
# Connect to Azure account (if not already connected)
Connect-AzAccount
# Get all VMs in the subscription
$vms = Get-AzVM
# Array to store results
$results = @()
foreach ($vm in $vms) {
# Get the OS disk
$disk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name
# Create result object
$result = [PSCustomObject]@{
VMName = $vm.Name
ResourceGroup = $vm.ResourceGroupName
CreationTime = $disk.TimeCreated
}
# Add to results array
$results += $result
}
# Display results
$results | Format-Table -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment