Oct 31

Setting ESXi DNS and NTP using PowerCLI

This is the second post in a series of PowerCLI scripts I use when doing new vSphere implementations. The first script created a new Data Center in vCenter Inventory and added a list of ESXi hosts to the newly created Data Center.

After connecting to vCenter using Connect-VIServer run this PowerCLI script to configure the primary and alternate DNS servers, the domain name, and the NTP settings of each ESXi host in the vCenter Inventory. The script first prompts for the DNS, domain name, and NTP server information. Get-VMHost retrieves a list of the ESXi host in inventory and then applies the settings to each host. Once the configurations are applied the script enables the NTP Client and restarts NTP so that the hosts will receive time from the configured NTP sources.

# PowerCLI Script to Configure DNS and NTP on ESXi Hosts
# PowerCLI Session must be connected to vCenter Server using Connect-VIServer

# Prompt for Primary and Alternate DNS Servers
$dnspri = read-host "Enter Primary DNS"
$dnsalt = read-host "Enter Alternate DNS"

# Prompt for Domain
$domainname = read-host "Enter Domain Name"

#Prompt for NTP Servers
$ntpone = read-host "Enter NTP Server One"
$ntptwo = read-host "Enter NTP Server Two"

$esxHosts = get-VMHost

foreach ($esx in $esxHosts) {

   Write-Host "Configuring DNS and Domain Name on $esx" -ForegroundColor Green
   Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dnspri , $dnsalt -Confirm:$false

   Write-Host "Configuring NTP Servers on $esx" -ForegroundColor Green
   Add-VMHostNTPServer -NtpServer $ntpone , $ntptwo -VMHost $esx -Confirm:$false

   Write-Host "Configuring NTP Client Policy on $esx" -ForegroundColor Green
   Get-VMHostService -VMHost $esx | where{$_.Key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false

   Write-Host "Restarting NTP Client on $esx" -ForegroundColor Green
   Get-VMHostService -VMHost $esx | where{$_.Key -eq "ntpd"} | Restart-VMHostService -Confirm:$false

Write-Host "Done!" -ForegroundColor Green

The script requires that two DNS and two NTP servers are configured but could easily be modified to only require one (not really a best practice but sometimes that is just the way it is). It will produce an error if only one DNS or NTP server is entered, there are no checks for this but that could be added.

Much faster than accessing the configuration of each host using the vSphere Client and the script also ensures these configurations are consistent across all hosts in inventory.

Hope you find it useful. Comments or recommendations for improvement are always welcome.

About the author


Hersey Cartwright is an IT professional with extensive experience designing, implementing, managing, and supporting technologies that improve business processes. Hersey is Solutions Architect for SimpliVity covering Virginia, Washington DC, and Maryland. He holds the VMware Certified Design Expert (VCDX-DV #128) certification. Hersey actively participates in the VMware community and was awarded the VMware vExpert title in 2016, 2015, 2014, 2013, and 2012. He enjoys working with, teaching, and writing about virtualization and other data center technologies. Follow Hersey on Twitter @herseyc


Skip to comment form

  1. Graham F French

    Hi Hersey,

    Thanks for the excellent script. I often find small errors creeping in when my clients decide to setup the NTP and DNS settings manually. The NTP settings may not be immediately obvious that it’s incorrect and will sometimes only reveal itself as an issue when the clock drift is beyond 5 minutes.



  2. Mike Pagán

    As a coding exercise I was crudely working my way to what you created (setting NTP and DNS on hosts). I like the layout and user feedback you put in there. Thanks for sharing your script.

  3. Steve

    Does the script work on ESXi 6.x or would it require modification?

    1. vHersey

      It should work with ESXi 6.x

  4. Abhishek Singh

    I’d like to check if my DNS configuration on Vcenter is perfectly done. I’d like to check DNS from IP as well as from Configuration settings. Could you please help me on this. many Thanks.

    Abhishek Singh

  5. ravi

    Hello Hersey,

    Is there any way to update the IP address and hostnames for ESXi servers using powercli.

    I am Installing the ESXi servers using AutoDeploy .Post deployment I would like to change the IP address and hostnames.

    1. vHersey


      Check this out. http://www.yellow-bricks.com/2013/05/27/how-to-change-the-ip-address-of-esxi-through-the-commandline/ Should get you going in the right direction.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may 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>

8 + two =