Useful commands PowerCli

Hi All,

I would like to share with you some useful PowerCli commands to work with VMware environments., The following commands will help you to automate your actions with Virtual Machines.

Clone Virtual Machines from specific Template on specific Host and Data store, the VM names taken on TXT file:

$Creds = get-credential 
$VMlist = get-content C:\Users\Administrator\Desktop\Servers.txt
$Datastore = "datastorename"
$Template = "TemplateName"

foreach ($vm in $VMlist)
    New-VM -vmhost hostname -Name $vm -Template $template -Datastore $Datastore 

Create snapshot on VM’s with same name, without memory:

$VMlist = get-content get-content C:\Users\Administrator\Desktop\Servers.txt
get-vm $VMlist | new-snapshot -Name "BeforeUpdate" -Description "BeforeUpdate" -Quiesce -Memory:$false

Remove snapshot on VM based TXT that name match is “BeforeUpdate”:

get-vm $VMlist | Get-Snapshot | where {$ -match "BeforeUpdate"}| Remove-Snapshot

Rename Windows name by running invoke “rename-computer” command:

$VMlist = get-content C:\Users\Administrator\Desktop\Servers.txt
$Creds = get-credential 
foreach ($vm in $VMlist){Invoke-VMScript -VM $vm -ScriptType Powershell -ScriptText "Rename-Computer -NewName $vm" -GuestCredential $Creds}

Restart VM’s from TXT name:

 (Get-VM $VMlist) | Restart-VM
About Meir Peleg 338 Articles
My name is Meir Peleg, (Born 30/04/1991) live in ISRAEL. Live and breath IT world. Microsoft MVP Windows IT & VMware vExpert.