How to Add ESXi Hosts to vCenter using PowerShell

There are quite a few clicks needed to add a host to vCenter. If you are deploying multiple hosts to your environment you have many clicks ahead. You should script it! This PowerShell script will make adding multiple hosts to vCenter easy!

Let’s see it in action!

#####################################################################
# Load VMware Plugins and connect to vCenter
#####################################################################

Add-PSSnapin vmware.vimautomation.core
## Enter your vCenter here
connect-viserver -server LABVC01.virtuallyboring.com

########################################################################
# Add Multiple Hosts to vCenter
######################################################################## 

# Variables
## You can use comma separated names or change to pull from a text file. Your pick.
$ESXiHosts = "ESXi01.virtuallyboring.com" , "ESXi02.virtuallyboring.com" , "ESXi03.virtuallyboring.com"
## Enter the name of a Data Center or Host Cluster
$ESXiLocation = "Nested ESXi Cluster"

# Start Script
$credentials = Get-Credential -UserName root -Message "Enter the ESXi root password"

Foreach ($ESXiHosts in $ESXiHosts) { 
 Add-VMHost -Name $ESXiHosts -Location $ESXiLocation -User $credentials.UserName -Password $credentials.GetNetworkCredential().Password -RunAsync -force
 Write-Host -ForegroundColor GREEN "Adding ESXi host $ESXiHosts to vCenter"
 } 
# End Script 

In my environment I deployed three new ESXi hosts (ESXi01-03.virtuallyboring.com) and I want to add them to my cluster named Nested ESXi Cluster:

PS Add Hosts to vCenter 1 - Empty Cluster

Open the script in PowerShell ISE as we need to update a few variables highlighted in yellow:

  1. Update the vCenter name to your vCenter name
  2. In the Variables section update $ESXiHosts to the names of the hosts you want to add
  3. Update the $ESXiLocation to the name of the Data Center or Host Cluster you want the hosts added to

You are now ready to run the script! You should receive a popup requesting the root credentials of the ESXi hosts. The password never enters plain text and remains encrypted and secure (See screenshot below):

PS Add Hosts to vCenter 4 - Script Output

Popup requesting ESXi root credentials:

PS Add Hosts to vCenter 2 - Credential Request

Over in vCenter you should see Add Host tasks with the hosts appearing under your specified location!

PS Add Hosts to vCenter 3 - Hosts Added

This script should make it very easy to add multiple hosts to your environment in one swoop!

One thought on “How to Add ESXi Hosts to vCenter using PowerShell

  1. Hi,
    This is krish here please update me after adding .vmx file into inventory the the vm should power on automatically using powershell

Leave a Reply