PowerCLI: One-liners last 10 VMs created and removed
I was asked if it was easy enough to get a list of the last 10 VMs created in a virtual Infrastructure, the answer is yes, its a one-liner !
To list the last 10 VMs created, cloned or imported use the following:
Get-VIEvent -maxsamples 10000 |where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage -First 10
Or if you would like to get the last 5 VMs removed from your VI use the following:
Get-VIEvent -maxsamples 10000 | where {$_.Gettype().Name -eq "VmRemovedEvent"} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 19
As requested, here is how you can get a list of the VM’s created over the last 14 days:
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage
And a list of the VMs removed over the last 14 days:
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) |where {$_.Gettype().Name-eq "VmRemovedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage
PowerCLI: Number of vCPUs in a cluster PowerCLI: Mass provision datastore’s











Very cool, I really need to go back and look at all the new cmdlets. Half the stuff I still do with Get-View have real cmdlets now! I would suggest using the -match operator… Slightly faster and easier to read.
Get-VIEvent -maxsamples 10000 |
where-Object {$_.Gettype().Name -match “(VmCreatedEvent|VmBeingClonedEvent|VmBeingDeployedEvent)“} |
Sort-object CreatedTime -Descending |
Select-Object CreatedTime, UserName, FullformattedMessage -First 10
~Glenn
@Glenn Sizemore
Great idea with the regex, didnt think of that. Thanks for the comment. Love your blog.
Can we modify the script to show ALL VMs built in last time period given?
@vuong
This is one of the things included in my Daily Report script but I have also altered the post above to show how to do this.
How can I output the results to a text file?
thanks
command(s) > \path\to\file.name
Actually I would do….
command(s) | Out-File c:\path\filename.txt
The output command doesnt output anything… not sure why.
Is there a way to output this to HTML? or a CSV?