Powershell – Change Service Account Username and Password

In Powershell, we can change Windows Service Account username and password using WMI based powershell cmdlet Get-WmiObject and the WMI class Win32_Service. You can even easily change Service account infromation in Remote Computer.

 

Powershell: Change Service Account username and password

Use below sample powershell script to change service account username and password. Before using this script, change the user credentials and service name with your own values in the script.

# Change service user name and password 
#
$UserName = 'DomainUserName'  
$Password = 'MyPa55w0rd' 
$Service = 'TestService' #Change your own service name
$svc_Obj= Get-WmiObject Win32_Service -filter "name='$Service'"

$StopStatus = $svc_Obj.StopService() 
If ($StopStatus.ReturnValue -eq "0") {
Write-host "The service '$Service' Stopped successfully" -f Green
} Else {
Write-host "Failed to Stop the service '$Service'. Error code: $($StopStatus.ReturnValue)" -f Red
}

Start-Sleep -Seconds 10

$ChangeStatus = $svc_Obj.change($null,$null,$null,$null,$null,
                      $null, $UserName,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq "0")  {
Write-host "Log on account updated sucessfully for the service '$Service'" -f Green
} Else {
Write-host "Failed to update Log on account in the service '$Service'. Error code: $($ChangeStatus.ReturnValue)" -f Red
}


$StartStatus = $svc_Obj.StartService() 
If ($StartStatus.ReturnValue -eq "0")  {
Write-host "The service '$Service' Started successfully" -f Green
} Else {
Write-host "Failed to Start the service '$Service'. Error code: $($StartStatus.ReturnValue)" -f Red
}

Powershell: Change Service user name and password in Remote Server

Use the below sample powershell script to change service user name and password from remote computer. Before using this script, change the user credentials, remote server name and service name with your own values in the script.

# Change service username and password in Remote Computer
#
$UserName = 'DomainUserName'  
$Password = 'MyPa55w0rd' 
$Service = 'TestService' #Change your own service name
$computer = 'Your-Remote-PC' #Change your own server/computer name
#Prompt you for user name and password to access remote computer 
$Cred = Get-Credential 
$svc_Obj= Get-WmiObject Win32_Service -ComputerName $computer -filter "name='$Service'" -Credential $cred

$StopStatus = $svc_Obj.StopService() 
If ($StopStatus.ReturnValue -eq "0") {
Write-host "The service '$Service' Stopped successfully in $computer" -f Green
} Else { 
Write-host "Failed to Stop the service '$Service'. Error code: $($StopStatus.ReturnValue)" -f Red
}

Start-Sleep -Seconds 10

$ChangeStatus = $svc_Obj.change($null,$null,$null,$null,$null,
                    $null,$UserName,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq "0")  {
Write-host "Log on account updated sucessfully for the service '$Service' in $computer" -f Green
} Else {
Write-host "Failed to update Log on account in the service '$Service'. Error code: $($ChangeStatus.ReturnValue)" -f Red
}

$StartStatus = $svc_Obj.StartService() 
If ($StartStatus.ReturnValue -eq "0")  {
Write-host "The service '$Service' Started successfully in $computer" -f Green
} Else {
Write-host "Failed to Start the service '$Service'. Error code: $($StartStatus.ReturnValue)" -f Red
}

Advertisement

7 thoughts on “Powershell – Change Service Account Username and Password”

  1. Third if condition is wrong. Should be $StartStatus instead of $ChangeStatus.
    Else blocks that throw an exception should also be added.
    $Password should be set with string in single quotes.
    Sleep time between stop and start can also help if stopping needs some time, otherwise start fails because service is still running.

    Reply
    • You have to keep the multiple service names in string array and iterate one by one with the same commands.

      $Services = @(“Service1″,”Service2″,”Service3”)
      ForEach($Service in $Services)
      {
      #Put the all commands here and remove the existing $Service variable.
      }

      Reply

Leave a Comment