VMware PowerCLI One-Liners

Manage your Vmware Infrastructure with the power of a single command!

I love VMware One Liners, I first saw them on the Alan Renaulf blog, and I was amazed that you could do such massive requests with so little code. So I spent a lot of time trying to create my own; some I have adapted from others, but most of these were created by me as I am the definition of a Lazy SysAdmin, not lazy as in I don’t do any work, Lazy as in I hate doing repeatable work manually.

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 that are powered off.

PowerCLI
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, and it will search the Get-VIEvent logs and calculate VMs created in the last 10 days. Change the integer at the end of the command to vary the search criteria.

PowerCLI
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

PowerCLI
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 whether all your hosts are connecting to a reliable NTP source for their time.

PowerCLI
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 a Name, How many CPUs, Operating System, Service Pack Level

PowerCLI
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 are running on the hosts

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.

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

List All VM’s disk space.

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

PowerCLI
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

PowerCLI
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 to. 

PowerCLI
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 to. 

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

Query ESX Host DRS Status

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

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

Check HA Status Levels

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

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

Get ESX host’s Name, IP, Subnet, Gateway, and DNS configuration.

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

Get a Multipath Policy of mapped storage.

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

Get ESX host NTP Configuration settings.

PowerCLI
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 data centre

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

Get ESX name and the Dell Service Tag.

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

This will dump the Hostname and the Dell Service Tag values across all 3 identifiers.

PowerCLI
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

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

That’s it. Thanks for taking the time to read this article. if you have any questions or feedback, please write in the comment section below.

Elsewhere On TurboGeek:  Who & What is Hashicorp? How Did It Began?

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »