Reset ESXi root account w/ PowerCLI

This method of resetting the root password does not require you to know the existing password and is a lot easier that setting up a host profile to change the passwords.

Original script source & writer – the best script is one someone else already wrote 🙂

# https://www.linkedin.com/pulse/reset-esxi-root-password-through-vcenter-esxcli-method-buschhaus
# First, setup $vmhosts. You can do this many ways.$vmhosts = Get-Cluster -Name ClusterWithUnknownPassword | Get-VMHost
# Just so it contains one or more VMHost objects.
# To reset all ESXi host passwords use
# $vmhosts = Get-VMHost
# $vmhosts = Get-Cluster -Name "Cluster Name" | Get-VMHost

# Since this only works on ESXi 6 and up I used this option to skip the 5.5 hosts that will error out. -AD
$vmhosts = get-vmhost |Where-Object {$_.Version -eq '6.0.0'}

# This will prompt for the new root password -AD
$NewCredential = Get-Credential -UserName "root" -Message "Enter an existing ESXi username (not vCenter), and what you want their password to be reset to."
Foreach ($vmhost in $vmhosts) {
    $esxcli = get-esxcli -vmhost $vmhost -v2 #Gain access to ESXCLI on the host.
    $esxcliargs = $esxcli.system.account.set.CreateArgs() #Get Parameter list (Arguments)
    $esxcliargs.id = $NewCredential.UserName #Specify the user to reset
    $esxcliargs.password = $NewCredential.GetNetworkCredential().Password #Specify the new password
    $esxcliargs.passwordconfirmation = $NewCredential.GetNetworkCredential().Password
    Write-Host ("Resetting password for: " + $vmhost) #Debug line so admin can see what's happening.
    $esxcli.system.account.set.Invoke($esxcliargs) #Run command, if returns "true" it was successful.
}

Leave a comment