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.

13 responses to “Changing ESXI host’s forgotten root password using PowerCli”

  1. Hi Ankush, with your help I am able to reset the ESXi password without painful, Thank you very much for your support.


  2. PS /root> Set-VMHostPassword -VMHost (Get-VMHost psz-esxi-mid.psz.corp.pegatron) -UserName root -Password xxxxxxxxx
    InvalidOperation: /root/test.ps1:44
    Line |
    44 | $IDList=$esxcli.system.account.list.invoke().UserID
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | You cannot call a method on a null-valued expression.

    Set-VMHostPassword: Entered Username does not exist in esxi userid list

    esxi version 5.5

    How to fixed it ?


  3. Hi Ankush, very usefull function. Thank You.
    one question. how can i use it against multiple hosts,
    Exmample: a csv file with list of esxi hosts or all the esxi hosts in the vcenter.


    • It depends on requirement , whether all esxi we want to set same password or not
      If we want to set same password for all then we could run this in for loop , if we need to set different password for each host then we need to take route of CSV


  4. Get-VMHost | foreach { Set-VMHostPassword -VMHost $ -UserName root -Password Password123!

    Would this change all esxi hosts at one command without putting host names from CSV ?


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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

%d bloggers like this: