Quick and Easy Powershell one-liners

This will be an evolving list as I find good and simple one-liner powershell commands:
  • Get-VM | Update-Tools -NoReboot 
    • As you can tell this one updates the vmtools on the VMs (caveat: doesn't always work with certain clients)
  • Get-VM | Get-Snapshot | where {$_.Created -lt "mm/dd/yyyy"} | fl *
    • Snapshots in VMware is a royal pain in the neck, this is a one liner that helps discover any snapshots older than mm/dd/yyyy
  • Get-VM | Get-Snapshot | where {$_.Created -lt "mm/dd/yyyy"} | Remove-Snapshot
    • Yep, the same as the last one but this one merges the snapshot into the main .vmdk
  • Get-PowerCLIVersion
    • It is essentially that, get the version of the PowerCLI installed and the attached snapins
  • get-vm <vmname> | get-vmhost
    • This shows a specific vm is on which VMhost
  • foreach ($vhost in get-cluster <cluster name> | get-vmhost) {$vhost | new-datastore -Nfs -Name <nfs datastore name> -Path "< path >" -NfsHost <hostip>}
  • get-service | where {$_.DisplayName -match "VMware"}
    • This is one of the best ones for the vCenter server, when you want to check the status of your vCenter Services.
  • get-vm <named VM>| suspend-vm
    •  suspends the named VM **be careful running this in production without the <named VM> variable as it will suspend ALL VMs in the datacenter
  • get-vmhost <VC> | get-vm | where {$_.Name -notmatch "<Guest to Exclude>"} | move-vm -destination (get-vmhost <ESX Server to move to>)
  • Get-VM <VM Name> | Set-VM -numCPU 2 -MemoryMB 4096
    • Use this when you have hot-add CPU and Memory enabled and you can add it live.
  • Get-VM | Select name, @{N="SyncWithHost"; E=$_.extensiondata.config.tools.synctimewithhost}
  • Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, ` @{N="ESX Host";E={Get-VMHost -VM $_}}, ` @{N="Datastore";E={Get-Datastore -VM $_}} 
    • This one is a slick one from ICT-Freak.nl and produces wonder output of VM | Cluster | Host | Datastore

    Comments