Use basic and advanced Cmdlets to manage VMs and ESXi Hosts – Part1 {Managing VM}

In this note I did the following:

Force power Off & power on VM
Create VM with various parameters
Get All Virtual machines having a CD drive connected
Add a network adapter and HardDisk
Increase virtual machine memory
Creating a clone of a VM
Convert a VM cloned to a template
Create a VM snapshot
Get & upgrade VM Hardware version
Get or list Virtual machines on a datastore
Remove specific Harddisk in a VM
Delete a specific “single” Snpashot from a virtual machine
Connect & disconnect CD ISO
Remove VM from inventory
Moving VM to another host

Force power VM as there is no vmare tools installed to gracefully shut it down:

PowerCLI C:> Stop-VM xp-01

Starting it up:

Create VM , Given CPU , Memory , Disk Size , storage format, specific DatasStore specific Guest OS type in particular Folder location & vm hardware version

PowerCLI C:> New-VM -Name Test02 -VMHost -NumCpu 2 -MemoryMB 2048 -DiskGB 5 -DiskStorageFormat thin -Datastore ds01 -GuestId windows8Server64Guest -Location CliVMs -Version v8

From GUI:

Another view to list VM hardware:

For the deprecated cmdlets :

NB: to know what OS type, have a look here: Enum – VirtualMachineGuestOsIdentifier

Get All Virtual machines having a CD drive connected :

Get-CDDrive -VM $VMs | Where-Object {$_.ConnectionState.connected -eq “true”} | Export-Csv C:SourcegetCDdrviveConnected.csv

For displaying on

A nice script is here:

Disconnect all VMs where the CD Drive is connected and it is not an .ISO

From <>

I am sure you will like this table πŸ™‚

Floppy Drives CD/DVD Drive USB Device Ethernet Adapter Hard Disk SCSI Device




Get-USBDeviceRemove-USBDevice Get-NetworkAdapterNew-NetworkAdapterRemove-NetworkAdapter






From <>

Let us add a network adapter and HardDisk

First we know what we have:

Or with view:

PowerCLI C:> Get-VM xp-01 | Get-View

Increase virtual machine memory:

PowerCLI C:> Set-VM -vm test01 -MemoryGB 1

It might not work if the VM is powered on , and no vmtools installed , or the hot add for memory or cpu is enabled , vpshere web client is where you go , or setting advanced options, but you have to shut it down first ,

PowerCLI C:> Get-Vm -Name xp-01 | Get-HardDisk | fl

PowerCLI C:> Get-VM -name xp-01 | New-HardDisk -CapacityGB 1

Finally it is done

Creating a clone of a VM

My lovely green progress bar

From GUI it works

Let us convert it the virtual machine created clone to a template :

PowerCLI C:> get-vm -Name XP-01Clone | Set-VM -ToTemplate

Creating Snapshots for All VMs πŸ™‚

PowerCLI C:> $AllVMsSnaps= Get-VM

PowerCLI C:> get-vm $AllVMsSnaps | New-Snapshot -Name “All VMs Snaps :)”

Confirm from GUI

List all Snapshots created in your Vmware:

PowerCLI C:> get-vm $AllVMsSnaps | Get-Snapshot

Or get it in more details:

Get VM Version:

PowerCLI C:> get-vm xp-01 | select Name, version |FL

Upgrading hardware version:

PowerCLI C:> get-vm -Name test02 | Set-Vm -Version v10

But you can not reconvert it back to version 8:

Can we get VMs stored in specific Data store ?

PowerCLI C:> Get-Datastore

PowerCLI C:> get-vm xp-01 | Get-HardDisk |fl

A summarized view:

We need to remove the Hard disk we just add:

PowerCLI C:> Get-HardDisk -VM xp-01 |fl

Note the highlighted:

You have to be more specific when you are deleting:

PowerCLI C:> Get-HardDisk -VM xp-01 | Where-Object {$_.Name -eq “Hard disk 2”}

The error stating that you need to remove the snapshot associated with that HD first:

[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is “Y”):

Remove-HardDisk : 4/11/2015 12:11:13 PM Remove-HardDisk The operation for the entity “XP-01” failed with the following message: “Invalid configuration for device ‘0’.”. Cannot remove virtual disk from the virtual machine because it or one of its parent disks is part of a snapshot of the virtual machine.

At line:1 char:84

+ Get-HardDisk -VM xp-01 | Where-Object {$_.Name -eq “Hard disk 2”} | Remove-HardDisk <<<<

+ CategoryInfo : NotSpecified: (:) [Remove-HardDisk], InvalidDeviceSpec

+ FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveHardDisk

So let us delete the snapshots for this VM ONLY:

PowerCLI C:> Get-VM -Name xp-01 | Get-Snapshot | fl

PowerCLI C:> Get-Snapshot -VM xp-01 | Remove-Snapshot | Where-Object {$_.Name -eq “All VMs Snaps :)”}

My lovely green color πŸ™‚

Now we back to delete the Hard Disk:

PowerCLI C:> Get-HardDisk -VM xp-01 | Where-Object {$_.Name -eq “Hard disk 2”} | Remove-HardDisk

So as above ,, it has a single Hard disk.

Disconnect & Connect CD Iso:

PS C:> get-vm UM-tst | Get-CDDrive |fl

PS C:> get-vm UM-tst | Get-CDDrive | where {$_.ISOPATH -ne $null} | set-cddrive -nomedia

PS C:> get-vm UM-tst | Get-CDDrive |fl

Now I will try to remove a VM from Inventory , then add it back, but I will need first to collect some information to keep in mind:

PowerCLI C:> Get-VM Test02 | Get-HardDisk |fl

So it is on ds01 ,, that is all what I want to know.

I will check the host where it resides , and try to filter it ONLY to remove it ONLY:

PowerCLI C:> Get-VMHost |Get-Inventory

Now it is time to remove our single VM by below command:

PowerCLI C:> Get-VMHost |Get-Inventory | Where-Object {$_.Name -eq “Test02”} | Remove-Inventory

From GUI:

Note that VM is not deleted it’s just removed from inventory:

You can see the files:

Now let us add it back:

PowerCLI C:> $VMXFile = “[ds01] Test02/Test02.vmx”

PowerCLI C:> New-VM -VMFilePath $VMXFile -VMHost -RunAsync

And from GUI

Moving VM to another host:

PowerCLI C:> Get-VM test02 |Move-Vm -Destination

My lovely green πŸ™‚

From GUI


About Ahmad Sabry ElGendi
This entry was posted in Uncategorized, VCAP5-DCA, Vmware. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s