PowerCLI One Liners

I am assuming that you are connected to vCenter server(s) in CLI to execute these commands. If not already you can do so with the below cmdlet

Connect-VIServer ‘VCName’

 

To query VM’s hardware version and VMware tools version:

Get-VM ‘VMName’ | Select Name,Version,@{N=”ToolsVersion”;E={$_.Extensiondata.Config.Tools.ToolsVersion}}

Get-VM (Get-Content .\Input.txt) | select Name,Version,@{N=”ToolsVersion”;E={$_ | Get-VMGuest | select -ExpandProperty ToolsVersion}} | Export-Csv -NoTypeInformation -Path .\Result.csv

 

To query network adapter type on list of VMs:

$list = Get-Content .\VMs.txt; foreach($line in $list) { Get-VM $line | Get-NetworkAdapter | Select @{N=‘VM’;E={$line}},Name,Type | Export-Csv  -path .\Report.csv -NoTypeInformation -Append }

 

To update network adapter type on a VM(experimental):

The below cmdlet changes network adapter from ‘E1000’ to ‘VMXNET3’. Though the changes reflect in VM level I see corresponding drivers are not getting updated in OS. Please comment if it works for you.

Get-VM VMName | Get-NetworkAdapter | Where { $_.Type -eq “E1000”} | Set-NetworkAdapter -Type “vmxnet3” -confirm:$false

 

To add portgroup:

The below cmdlet can be used to create a VM portgroup on all the hosts in a standard vSwitch cluster

Get-Cluster ‘ClusterName’ | Get-VMHost | foreach { New-VirtualPortGroup -Name ‘PGName’ -VirtualSwitch (Get-VirtualSwitch -Name vSwitch0 -VMHost $_) -VLanId 1000 -Confirm:$false }

 

To query Independent disks on VMs:

The below cmdlet can retrieve persistence mode of VM harddisks. Useful particularly when you want to identify Independent mode harddisks which cannot be backed up by snapshot based backup applications.

Get-Cluster ‘ClusterName’ | Get-VM | Get-HardDisk | Select Parent,Name,Persistence | Export-Csv -Path .\Report.csv -NoTypeInformation

 

To query the available drives and disk usage on VMs:

(Get-VM vmname).Guest.Disks |  Select Path,CapacityGB,FreespaceGB

 

The below line can grab the details for all the VMs in a vCenter

Get-VM | where {$_.PowerState -eq ‘PoweredOn’} | foreach{$vmname = $_.Name; $_.Guest.Disks | Select @{N=‘VMName’;E={$vmname}},Path,CapacityGB,FreespaceGB | Export-Csv -NoTypeInformation -Path .\Result.csv -Append}

 

To query if any memory limit configured for the VM:

Note: -1 in MemLimitGB column indicate no limit set for the VM (‘Unlimited’ checkbox ticked under Resource allocation section)

Get-VM ‘VMName’ | Select Name,MemoryGB,@{N=‘MemLimitGB’;E=$_ | Get-VMResourceConfiguration | select -ExpandProperty MemLimitGB }}