Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

Top 5 PowerCLI Cmdlets that every VMWare Administrator should know!

Posted on 2 October 20214 March 2024 by VirtuallyThatGuy

This is the top five powercli commands every VMware SA should have or know. I would also highly recommend starting out in the vSphere Documentation Center. It’s not really what I would consider bedtime reading but you can find some excellent new cmdlets and see usage.

1. Get-VM

It’s a classic. It has been around for awhile and we have to start with this one. Good old Get-VM! It is a great place to start. It can produce a simple inventory of all VMs that are currently found within vCenter . Using the Get-VM cmdlet, and the standard PowerShell Select-Object cmdlet, you can retrieve the particular properties that are of interest to you and use Export-CSV to export that data into a CSV file for further data manipulation.

Get-VM | Select-Object Name,NumCPU,MemoryGB,PowerState,VmHost 
Export-CSV VMs.csv -NoTypeInformation

You can also use Get-VM to find snapshots you might have forgotten about in your environment.

Get-VM | Get-Snapshot | format-list | out-file c:\snapshots.txt

2. Move-VM

If you’re looking to move all the VMs from one host to another for possible maintenance, you can use the Move-VM cmdlet. You can also use Maintenance Mode as well, but we’re focusing on the CLI here! Below we are taking VMs on esxi01 and moving them to esxi02.

Get-VMHost lab-esxi01.labl.local | Get-VM | Move-VM -Destination (Get-VMHost lab-esxi02.lab.local)

Maybe you’re only interested in live migrating a single VM? This command will take a VM called MyVM and move it to the esxi01 host.

Move-VM -VM 'MyVM' -Destination 'lab-esxi01.lab.local'

With the command below, you can also do a storage vMotion. MyVM is the VM name and MyDatastore is your destination datastore.

Move-VM -VM 'MyVM' -Datastore 'MyDatastore'

3. New-VM

If you’re looking to build a single VM, you can use New-VM using your information to create a single VM. This basically builds a blank VM, you’ll have to deploy the OS, etc. You can see below for other options to deploy multiple VMs and use templates and customization specifications instead.

New-VM –Name “MyVM” –VMHost esxi01 –ResourcePool Production –DiskGB 20 –DiskStorageFormat Thin –DataStore datastore1 –MemoryGB 3 –NetworkName “ProdNetwork”

This is one I have used a lot in testing environments where you are cloning machines all the time. What this does below is creates a new machine called NewVM8 from a clone of OriginalVM. It will deploy your VM to the esxi01 host, although the ResourcePool parameter accepts ResourcePool, Cluster, vApp, and standalone VMHost objects so you can use any of those.

$myResourcePool = Get-ResourcePool -Name esxi01
$mySpecification = Get-OSCustomizationSpec -Name WindowsSpec
New-VM -VM OriginalVM -Name NewVM -OSCustomizationSpec $mySpecification -ResourcePool $myResourcePool

If you’re not wanting to run a script for each VM, it is possible to create multiple VMs from a CSV. This is a relatively basic way to get the job done. For multiple VMs, create a CSV like this.

Then after you have the CSV, you can use the New-VM cmdlet below and deploy multiple VMs. For this I typically don’t go crazy as it taxes vCenter if you have a ton of VMs.

$vms = Import-CSV C:\Scripts\NewVMs.csv
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization `
-Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync
}

4. Invoke-VMScript

How many times have you logged into a VM to run a script inside the guest operating system, only to logout again a few minutes later? This Invoke-VMScript cmdlet will save you the hassle of doing that.

The script below will run a BAT script. In BAT scripts, to access environment variables, you must use the following syntax: %<environment variable>% (for example, %programfiles%).

The outer quotes ($script = ‘…’) are required because this is how you define a string variable in PowerShell. The inner double quotes are required because there are spaces in the path.

$script = '"%programfiles%\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "%tmp%\inforeport"'

Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential -ScriptType Bat

5. Get-VMHost

A common issue in virtual environments is NTP. Hosts are out of sync, then the login issue creep up. Performance monitoring quits working, etc. It’s overlooked a lot. You can use Host Profiles if you’ve got Enterprise Plus licensing, but you can also use the command below to modify and change the NTP settings on a host.

Get-VMHost esx01 | Add-VMHostNtpServer -NtpServer ntpservername

# Another good reason to use Get-VMHost would be to easily find what host a VM is currently running on.

$MyVM = Get-VM -Name MyVM
Get-VMHost -VM $MyVM

Wrap up:

To wrap up, these are just a few of the awesome cmdlets you can use, but these 5 are ones that every single VMware administrator should know and commit to your brain. VMware is always adding new features but these are a few you should commit to memory. If you’re a fan of other ones post them in the comment section and let us know how you use them.

2 thoughts on “Top 5 PowerCLI Cmdlets that every VMWare Administrator should know!”

  1. Dazza says:
    22 November 2021 at 10:11

    Where’s the link to the CSV file?

    Reply
    1. VirtuallyThatGuy says:
      22 November 2021 at 11:26

      Dazza, which csv are you referring to? If VM creation script, then refer to previous post https://virtuallythatguy.co.uk/create-vms-using-powercli-virtuallythatguy/

      csv content


      Name Template Host Datastore Customization
      Ron-Test01 Win2019 lab-esxi01 lab-datastore-01 lab-join-ad
      Ron-Test02 Win2019 lab-esxi02 lab-datastore-02 lab-join-ad
      Ron-Test03 Win2019 lab-esxi03 lab-datastore-03 lab-join-ad
      Ron-Test04 Win2019 lab-esxi04 lab-datastore-04 lab-join-ad
      Ron-Test05 Win2019 lab-esxi05 lab-datastore-05 lab-join-ad

      Reply

Leave a Reply Cancel reply

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

When autocomplete results are available use up and down arrows to review and enter to go to the desired page. Touch device users, explore by touch or with swipe gestures.

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • JB on Script: How to get VM with Tag Assignment and export results to csv using PowerCLI or Powershell
  • DL on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • 360coolp on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • Yogesh on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2025 VirtuallyThatGuy | Powered by Superbs Personal Blog theme