Unable to update log on credential of service via powershell

Deepa Annie 20 Reputation points

Powershell verision is 5.1

i am able to update the username and password manually of a server.

I am unable to achieve that through powershell.

tried the below. am getting return value 15 when i start
$account = "<the account name>" $password = "<the account password>" $servicename = "name='<the service name>'" $svc = gwmi win32_service -filter #--returnvalue is 0

$servicename $svc.StopService() #--returnvalue is 5

$svc.change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null) $svc.StartService() #--returnvalue is 15

i am unable to start it manually when the credential is set through powershell

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,970 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,085 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 27,376 Reputation points

    The account that you use needs to have the "logon as service" right. If you want to do this strictly from a command line, then my preferred solution would be the ntrights program from the Windows 2003 Resourse Kit. Unfortunatly Microsoft has chosen to remove that download, so you will need another solution.

    Here's one.



    Download Get-UserRights and Set-UserRights.

    $user = "YourDomain\YourUser"
    $pswd = "password" 
    $svc = "YourServiceName" 
    Set-UserRights.ps1 -AddRight -UserRight SeServiceLogonRight -Username $user 
    sc.exe config $svc obj= $user password= $pswd
    Start-Service -Name $svc 
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 22,191 Reputation points Microsoft Vendor


    The return value 15 indicates Service Logon Failed. Did you provide the correct account and password?


    Or you can give a try to sc.exe.


    Best Regards,

    Ian Xue

    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.