Tinus EngOps Wiki


OpsaC - Operating as PowerShell code



PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Query VirtualMachines

tags: PowerShell categories: VMware

Table of Contents

autoauto- Table of Contentsauto- PowerCLIauto - Get-View vs. Get-VMauto - Fast listing of old Snapshotsauto- See alsoautoauto


Get-View vs. Get-VM

The performance with Get-View is much better than Get-VM:

Connect-ViServer -Server "vcenter.example.com"

$Stopwatch = [System.Diagnostics.Stopwatch]::new()

$OverheadVMs = Get-VM
$OverheadVMs | Get-Snapshot | Select-Object -Property VM, Name, Description



0.8237531 seconds
$Stopwatch = [System.Diagnostics.Stopwatch]::new()

$FastVMs = Get-View -ViewType VirtualMachine
$FastVMs | Select-Object -Property @{N='VM';E={$PSItem.Name}},
 @{N='Snapshot';E={$PSItem.Snapshot.RootSnapshotList[0].Name}} | Where-Object Snapshot



0.1823357 seconds

Fast listing of old Snapshots

$Stopwatch = [System.Diagnostics.Stopwatch]::new()

$FastVMs  = Get-View -ViewType VirtualMachine -Filter @{'Config.Template' = 'false'; 'Snapshot' = '.*'}
$Snapshot = $FastVMs | ForEach-Object {
    $CreateTime = Get-Date ($_.Snapshot.RootSnapshotList[0].CreateTime)
    [PSCustomObject][Ordered] @{
        CreateTime       = Get-Date ($CreateTime) -Format 'yyyy-MM-dd HH:mm:ss'
        VMName           = $_.Name
        Snapshot         = $_.Snapshot.RootSnapshotList[0].Name -replace '%2f', '/'
        Description      = $_.Snapshot.RootSnapshotList[0].Description
        TotalDays        = [Math]::Round((New-TimeSpan -Start $CreateTime -End (Get-Date)).TotalDays,0)
        ChildSnapshot    = $_.Snapshot.RootSnapshotList[0].ChildSnapshotList
        ChildDescription = $_.Snapshot.RootSnapshotList[0].ChildSnapshotList.Description
        ChildCreateTime  = $_.Snapshot.RootSnapshotList[0].ChildSnapshotList.CreateTime
} | Sort-Object CreateTime 
$Snapshot.Where( {$_.Description -notmatch 'do not delete' -and $_.TotalDays -gt $day} ) | Format-Table -AutoSize


See also

API Reference on VMware Developper Documentation.

[ Top ] [ Blog ]