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 {$_.name -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