PowerCLI – Useful single liners

Vm’s and space details

Get-Cluster <ClusterName>|get-vm|select Name,@{Name="ProvisionedSpace";E={[Math]::Round(($_.ProvisionedSpaceGB),2)}},@{Name="UsedSpace";E={[Math]::Round(($_.UsedSpaceGB),2)}}

Find Thick provisioning Vm’s in a datastore

Get-Datastore  | Get-VM | Get-HardDisk | Where {$_.storageformat -eq "Thick" } | Select Parent, Name, CapacityGB, storageformat | FT -AutoSize

Find VM’s migration failed VM’s (in last 5 days)

Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-5 ) | Where {$_.FullFormattedMessage -like "Cannot migrate*"} |select CreatedTime, Net, FullFormattedMessage |export-csv e:\cannot.csv

Find the Specific events across multiple hosts in a vCenter
(Below example we are checking for Down state)

Get-cluster|get-vmhost|Get-VIEvent -MaxSamples ([int]::MaxValue)|Where {$_.FullFormattedMessage -match "Down state"} |select @{Name='Host';E={$_.Host.name}},@{N='Createdtime'; E={$_.CreatedTime}},@{N='Description';E={$_.FullF
ormattedMessage}} |Export-Csv -Path c:/temp/Downstate.csv -NoTypeInformation -UseCulture

Find a Specific OS VM’s count
(Below case we are looking for Windows 7 vm’s)

Get-cluster|select Name,@{N="Windoes7-VMCount" ;E={(((get-cluster $_|get-vm).Extensiondata.guest.guestfullname -like "*Windows 7*")|Measure).count}}

Find OS details of VM’s – faster output

Get-Cluster <clustername>|Get-VM | Sort | Get-View -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") |?{$_.Config.GuestFullName -match "Server"}| Select -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}
},  @{N="Running OS";E={$_.Guest.GuestFullName}},@{N="cluster";E={(get-vm ($_.name)|get-cluster).name}} |Format-Table -AutoSize

Find a VM IP address

Get-VM|Select Name, @{N="IP Address";E={@($_.guest.IPAddress[0])}}

Find a vm product version (if specified)
(Below case we are trying to find Horizon UAG VM’s version)

Get-vm *UAG*|?{$_.powerstate -eq "poweredon"}|select name,@{N="UAG Version";E={$_.ExtensionData.summary.config.product.version}}

Find GPU Profile of the VM’s

Get-cluster |get-vm|select Name,NumCPU,MemoryGB,Powerstate,@{N="GPU Profile";E={($_.ExtensionData.Config.hardware.Device.backing.vgpu)}},@{N="CapacityGB";E={(get-vm $_|get-harddisk).CapacityGB}},@{N="storageformat
";E={(get-vm $_|get-harddisk).Storageformat}},@{N="HDD/Disk Type";E={(get-vm $_|get-harddisk).Persistence}},@{N="Host";E={(get-vm $_|get-vmhost).name}}|ft -AutoSize

Find Esxi hosts Names with VMK’s in a specific segment
(Below case we are looking for Esxi hosts having 172.1.1 Subnet in VMK’s)

Get-VMHost -state Connected|Get-VMHostNetworkAdapter -VMKernel |select @{N="Cluster";E={($_.vmhost|get-cluster).name}},vmhost,Name,IP|?{$_.IP -match "172.1.1"} |ft -AutoSize

Find VM details – Who/When provisioned and etc
(Below case we are looking for VM name contains test)

Get-vm *test*|select Name,@{N="Cluster";E={(get-vm $_|get-cluster).name}},Folder,NumCpu,MemoryGB,Powerstate,@{N="Hostname";E={$_.ExtensionData.Guest.Hostname}},@{N="IPaddress";E={$_.ExtensionData.Guest.IpAddress}},,@{N="Networkadapter";E={(get-vm $_|Get-NetworkAdapter).networkname}},@{N="GuestFullName";E={$_.ExtensionData.Guest.GuestFullName}},@{N="Boottime";E={$_.ExtensionData.Summary.Runtime.Boottime}},@{N="CreatedTime";E={(get-vm $_|Get-VIEvent  -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "clone*"}).CreatedTime}},@{N="CreatedUser";E={(get-vm $_|Get-VIEvent  -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "clone*"}).Username}},@{N="Event";E={(get-vm $_|Get-VIEvent  -MaxSamples([int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like "clone*"}).FullFormattedMessage}}|export-csv C:\temp\testvminfo.csv

Find NSX logical Router Cluster, Datastore, CPU & Memory reservations

Get-Nsxlogicalrouter|select Name,@{N="Cluster";E={$_.appliances.appliance.resourcePoolName}},@{N="Datastore";E={$_.appliances.appliance.datastoreName}},@{N="CPU Reservation";E={$_.appliances.appliance.cpuReservation.reservati
on}},@{N="Memory Reservation";E={$_.appliances.appliance.memoryReservation.reservation}}|Sort-Object -Property Name|ft -AutoSize

Find NSX Edge Cluster, Datastore, CPU & Memory reservations

Get-NsxEdge|select @{N="VM-Name";E={$_.appliances.appliance.vmName}},@{N="Cluster";E={$_.appliances.appliance.resourcePoolName}},@{N="Datastore";E={$_.appliances.appliance.datastoreName}},@{N="CPU Reservation";E={$_.appliance
s.appliance.cpuReservation.reservation}},@{N="Memory Reservation";E_.appliances.appliance.memoryReservation.reservation}}|Sort-Object -Property Name|ft -AutoSize

VM’s Folder details – order by power state

Get-Cluster |get-vm|select Name,powerstate,@{N="Folder";E={$_.folder.name}},@{N="Root Folder";E={$_.folder.parent}}|Sort-Object -Property powerstate|ft -AutoSize

Snapshot faster output

Get-View -ViewType virtualmachine -Filter @{"Runtime.PowerState"="poweredOn"; "snapshot" = ""} -Property Name|% {get-vm -id $_.MoRef | get-snapshot}|select VM,Name,Powerstate,Description,Quiesced,@{N="Size";E={"{0:N2} GB" -f
($_.SizeGB)}},Created|ft -AutoSize

Find Disk consolidation required VM’s

(Get-View -ViewType virtualmachine -Property Name, Runtime |where-object {$_.runtime.consolidationneeded -eq "True"}).Name

Scroll to Top