Warning: include(/var/sites/e/enlow.co.uk/public_html/wp-includes/assets/script-loader-packages.php): failed to open stream: No such file or directory in /var/sites/e/enlow.co.uk/public_html/wp-includes/script-loader.php on line 239

Warning: include(): Failed opening '/var/sites/e/enlow.co.uk/public_html/wp-includes/assets/script-loader-packages.php' for inclusion (include_path='.:/usr/local/php-5.6.37-flock/lib/php') in /var/sites/e/enlow.co.uk/public_html/wp-includes/script-loader.php on line 239

Warning: Invalid argument supplied for foreach() in /var/sites/e/enlow.co.uk/public_html/wp-includes/script-loader.php on line 241
PowerCLI - Thoughts of Dev and Datacenter



This is a list of the short PowerCLI scripts I’ve found useful.


Comparing Standard Switches between Hosts

Change $host1 and $host2 to appropriate values and this one-liner will show any switches that are on $host1 but not $host2. This will not show what is on $host2 but not $host1.

Get-VirtualPortGroup -VMHost $host1 -Standard | ? { $pg1 = $_; return ((Get-VirtualPortGroup -VMHost $host2 -Standard | ? { $_.Name -eq $pg1.Name }).Count -eq 0) }

List of Hardware models (and number in use) across multiple vCenters

Given the array $vctrs, which contains a list of vCenter servers to examine, this script will collate all the hardware models in use and provide a tally of how many you have.

$models = @(); $vctrs | % { $vs = Connect-VIServer $_ -Credential $cred; $results = Get-VMHost -Server $vs | Group Model | Select Name, Count; Disconnect-VIServer -Server $vs -Confirm:$False; Write-Host $_; $results | ft; $results | % { $item = $_; $found = $false; $models | ? { $_.Name -eq $item.Name } | % { $_.Count += $item.Count; $found = $true; }; if ($found -eq $false) { $models += $item; } } }; $models | ft Name, Count


Autoupgrade Tools

Getting a list of VMs that are set to upgrade VMware Tools when the VM is restarted. Conversely, you can edit the line below to show the VMs that aren’t set to upgrade Tools.

Get-Vm | ? { $($_ | get-view).Config.Tools.ToolsUpgradePolicy -eq "upgradeAtPowerCycle" } | sort Name | select Name, GuestID

List of VMs with attached networks

This will get the list of all VMs and the networks that they are connected to.

Get-VM | select Name, @{L="Networks";E={$_.ExtensionData.Network | % { Get-View "$($_.Type)-$($_.Value)" | Select -ExpandProperty Name}}}

Trimming a list of VMs according to name

Simple one this is – if you have VMs which have an identifier in them, for example “dc” for domain controller then to search, it is best to convert the VM name to upper/lower case before doing a comparison.

Get-VM | ? { $_.Name.ToLower() -Like "*dc*"  }


This will show all the VMs in a vCenter that have a snapshot with their corresponding age.

Get-VM | ? { Get-Snapshot -VM $_ } | % { Get-Snapshot -VM $_ } | select VM, Name, @{L="Age";E={$($(Get-Date) - $_.Created).Days}}, @{L="Size";E={[Math]::round($_.SizeGB, 2);}} | sort Age -Descending