Vmware PowerCLI One Liners

These commands will save you a mountain of time, despite the formatting of this website they should all copy as single line commands.

Find VMs powered off

Get-VM | where {$_.powerstate -eq "PoweredOff"} | Sort-Object | get-harddisk | ft parent, capacityGB, Filename -autosize

Find Vms CREATED last X days

This one liner does exactly what it says, it will search the Get-VIEvent logs and calculate VMs created in last 10 days. Change the integer at the end of the command to vary the search criteria.

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

Find Vms REMOVED last X days

This is very similar to the above command, except it looks for Servers deleted in the last 10 days

Get-VIEvent -maxsamples 10000 | where {$_.Gettype().Name -eq"VmRemovedEvent"} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 10

Get Time on all hosts

This is useful to determine all your hosts are connecting to a reliable NTP source for their time.

get-vmhost | select Name,@{Name="Time";Expression={(get-view $_.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}}

 

Get Very Detailed VM Information

This command will give you details about a VM, such as Name, How many CPU, Operating System, Service Pack Level

Get-VM |Where {$_.PowerState -eq “PoweredOn“} |Sort Name |Select Name, NumCPU, @{N=“OSHAL“;E={(Get-WmiObject -ComputerName $_.Name-Query “SELECT * FROM Win32_PnPEntity where ClassGuid = ‘{4D36E966-E325-11CE-BFC1-08002BE10318}’“ |Select Name).Name}}, @{N=“OperatingSystem“;E={(Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select Caption).Caption}}, @{N=“ServicePack“;E={(Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select CSDVersion).CSDVersion}}

Count How Many Vms running on host

This command will tell you how many VMs you have running on a host. Useful if you are looking at spreading server load throughout a cluster.

Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}}, Name, @{N="NumVM";E={($_ | Get-VM).Count}} | Sort Cluster, Name

 

List All Vm’s and there disk sizes

This command gets a list of your VMs and displays each disk path, capacity and free space

ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

Get Host Hardware information

This is useful to under what host hardware you are managing. It will display the Hardware Vendor, CPU type, number of cores, number of CPU sockets,  CPU speed and Memory size

Get-VMHost |Sort Name |Get-View |Select Name, @{N=“Type“;E={$_.Hardware.SystemInfo.Vendor+ “ “ + $_.Hardware.SystemInfo.Model}},@{N=“CPU“;E={“PROC:“ + $_.Hardware.CpuInfo.NumCpuPackages + “ CORES:“ + $_.Hardware.CpuInfo.NumCpuCores + “ MHZ: “ + [math]::round($_.Hardware.CpuInfo.Hz / 1000000, 0)}},@{N=“MEM“;E={“” + [math]::round($_.Hardware.MemorySize / 1GB, 0) + “ GB“}}

Query ESX Host Management Networks

This is useful to see what networks your hosts are mapped too. 

Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Where {$_.ManagementTrafficEnabled} | Select Hostname, PortGroupName, IP, SubnetMask

 

Query ESX Host vMotion Networks

This is useful to see what vMotion networks your hosts are mapped too. 

Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Where {$_.vMotionEnabled} | Select Hostname, PortGroupName, IP, SubnetMask

Query ESX Host DRS Status

Quick command that states the hosts DRS status – can be run against multiple hosts if needed.

Get-VMHost | Get-Cluster | Select Name, DrsEnabled, DrsMode, DrsAutomationLevel

Check HA Status Levels

This command will check High Availability status on an ESX host or Cluster


Get-VMHost | Get-Cluster | Select Name, HAFailoverLevel, HARestartPriority, HAIsolationResponse

 

Get ESX hosts Name, IP, Subnet, Gateway and DNS configuration

Get-VMGuestNetworkInterface –VM VMNAME | Select VM, IP, SubnetMask, DefaultGateway, Dns

 

Get Multipath Policy of mapped storage

Get-VMHost | Get-ScsiLun | Select VMHost, ConsoleDeviceName, Vendor, MultipathPolicy

Get ESX host NTP Configuration settings

Get-VMHost | Sort Name | Select Name, @{N=”NTP”;E={Get-VMHostNtpServer $_}}

Get Dell Service Tags

I primarily use Dell Service tags in my day job, this tool is priceless if you are too lazy to walk to the datacenter

Get-VMHost | Get-View | foreach {$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}

Get ESX name and the Dell Service Tag

Get-VMHost | Get-View | Select Name, @{N=”Service Tag”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}

 

This will dump the Host name and the Dell Service Tag values across all 3 identifiers

Get-VMHost | Sort Name | Get-View | Select Name, @{N=”Tag 3”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}, @{N=”Tag 2”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}, @{N=”Tag 1”;E={$_.Summary.Hardware.OtherIdentifyingInfo[3].IdentifierValue}}

 

Get ESX Host BIOS version and date(s)

Get when planning firmware updates on your infrastructure

Get-View -ViewType HostSystem | Sort Name | Select Name,@{N="BIOS version";E={$_.Hardware.BiosInfo.BiosVersion}}, @{N="BIOS date";E={$_.Hardware.BiosInfo.releaseDate}}