Changing ESXI host’s forgotten root password using PowerCli

Often there are issues in environments where ESXi passwords are forgotten.
The only options to recover are using Host profile or Re-Installation.
If host profile feature is not available ,

I have come up this function in PowerCli which is combined with EsxCli.

To make use of this function there are some prerequisites.

1. We should be connected to vCenter in PowerCLI, below is the command to connect to vCenter.
Connect-VIServer vcentername -User username -Password password123
2. You should be using the PowerCli 6.3 or later version.Below is the command to check the PowerCli version.
3.Password length should be more than 7 characters to 40 characters.Please refer below link for Password policy.
function Set-VMHostPassword
Created by: Ankush Sethi
Recover the ESXI root/other user's Password
Function will recover the esxi root password using PowerCli
Enter the esxi Hotsname for which we need to recover the password.
Enter the username of esxi host.
Enter the new password for esxi host.
example 1>Set-VMHostPassword -VMHost (Get-VMHost -UserName root -Password VMware123! `
example 2>Get-VMHost|Set-VMHostPassword -UserName root -Password VMware123!
process {
$validation1=Get-VMHost $VMHost -ErrorAction Stop
Write-Error -Message "Please check the host is part of connected vCenter or not and try again" -ErrorAction Stop
If(($validation1.ConnectionState -eq "Connected") -or ( $validation1.ConnectionState -eq "Maintenance"))
$esxcli=Get-EsxCli -VMHost $VMHost -V2
If(($IDList -contains $UserName) -ne $true){Write-Error -Message "Entered Username does not exist in esxi userid list" -ErrorAction stop}
Write-Error -Message "ESXI is not connected or maintenance mode to perform the action" -ErrorAction Stop
If($output -eq $true)
Get-VIEvent -Entity (Get-VMHost $VMHost) -MaxSamples 1|?{$_.fullformattedmessage -match "Password"}|select UserLogin,Createdtime,Username,Fullformattedmessage|ft -AutoSize
$hostd=Get-Log -Key hostd -VMHost (Get-VMHost $VMHost)
$hostd.Entries|Select-String "Password was changed for account" |select -Last 1

Sample usage of function: With hostname from pipeline.

Get-VMHost EsxiName|Set-VMHostPassword -UserName root -Password Password123!

Sample usage of function: With all arguments defined

Set-VMHostPassword -VMHost (Get-VMHost ESXIName) -UserName root -Password Password123!

Output: Once password is change it will display the events from event log and Hostd logs.

How to use the Function

  1. Copy the code from above and paste it into Notepad.
  2. Save it with anyname with extension of .ps1
  3. Open the PowerCli and to go the directory where it is saved using cd command
  4. Run the command . .\FileName.ps1 (Note :There is a space between 2 dots.)
  5. Function is loaded in local session of PowerCli and you are good to use this as cmdlet.

6 thoughts on “Changing ESXI host’s forgotten root password using PowerCli

  1. Pingback: Resetting Forgotten ESXi password |

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s