VMware PowerCLI Snippets


This page is a Pastbin for PowerCLI Snippets I use. If you have any questions, feel free to comment.

Import/Export vCenter Roles

#Export Role
$role = Get-VIRole -Name "MyRole" |Get-VIPrivilege; $role.id -join ","

#Import Role (Use the output from the command above)
New-VIRole -Name "MyRole" -Privilege (Get-VIPrivilege -Id System.Anonymous,System.View,System.Read)

Create Profile

New-Item -Path $Profile -ItemType file -Force
notepad $profile

Load VMware Cmdlet

Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue"

Connect to vCenter Server

Connect-VIServer -server <Server> -user administrator -password <Password>

PowerShell Basics

Where statements

get-vm  | Where-Object PowerState -eq "PoweredOff"
get-vm  | ? PowerState -eq "PoweredOff"


Move ESXi Hosts between Datacenters or Clusters

Get-VMHost -Name <ESX> | Move-VMHost -Destination <Cluster or Datacenter>

Create VMKernel Port for vMotion

$esx = Get-VMHost <HOST>
$vs = Get-VirtualSwitch -Name "dvSwitch"
$pg = Get-VirtualPortGroup -Name "dvPortGroup"
New-VMHostNetworkAdapter -VMHost $esx -PortGroup $pg -VirtualSwitch $vs -IP <IP> -SubnetMask <MASK> -VMotionEnabled:$true

Create VMKernel Port for vMotion one-liner

New-VMHostNetworkAdapter -VMHost "<HOST>" -PortGroup "<PORTGROUP>" -VirtualSwitch "dvSwitch Lab" -IP <IP> -SubnetMask <MASK> -VMotionEnabled:$true

Add multiple Hosts to vCenter by Hostname Range

1..20 | Foreach-Object { Add-VMHost esx$_.virten.lab -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true}

Add multiple Hosts to vCenter by IP-Address Range

10..20 | ForEach-Object { [System.Net.Dns]::GetHostbyAddress("192.168.0.$_")  } |
select-object HostName | ForEach-Object { Add-VMHost $_.HostName -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true }

Add multiple Hosts to vCenter by Input Textfile

Get-Content hosts.txt | Foreach-Object { Add-VMHost $_ -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true}

#Migrate E1000 Adapter to vmxnet3

Get-VM -name "<VM>" | Get-NetworkAdapter | Where { $_.Type -eq "E1000"} | Set-NetworkAdapter -Type "vmxnet3"
  1. Very usefull for all that work with VMware all kind of versión, thanks man.

  2. hi

    can any one plz let me know how to add esxi server in domain through power cli.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.