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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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!
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
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