37

How to change user credentials of windows service from command line?

1
  • not programmer related, try serverfault.com for such a questions. Commented Jun 8, 2009 at 19:04

4 Answers 4

76
sc.exe config "Service Name" obj= "DOMAIN\User" password= "password" type= own

See Shortcut Setting Log-On Credentials for Windows Services » jonathanmalek.com.

@MattT points out that on Windows Server 2008R2 you have to add type= own, but prior to that version it isn't necessary.

In PowerShell 3+, you can avoid escaping the arguments with the stop-parsing symbol: --%

sc.exe --% config "Service Name" obj= "DOMAIN\User" password= "password" type= own
9
  • 4
    Is there any way to grant the user the "Log on as a Service" right from the command line as well?
    – ColinM
    Commented Feb 27, 2013 at 22:49
  • 1
    @Kiquenet sc.exe \\servername config … (the backslashes are required) technet.microsoft.com/en-us/library/cc990290.aspx
    – brianary
    Commented Oct 16, 2014 at 21:23
  • 1
    Note that special characters in passwords require escaping--in my case a % caused a service logon failure until I changed it to %%. See stackoverflow.com/a/27451200/550712.
    – Mark Berry
    Commented Dec 12, 2014 at 20:02
  • 4
    as of Windows server 2008R2 it is necessary to add the argument "type= own" (without the quotes) for this to work. Otherwise sc reports the error "The parameter is incorrect"
    – Matt T
    Commented Jul 21, 2015 at 13:13
  • 1
    After changing logon from this command, I faced issue while starting the service using "net start service_name". This happened because of logon service rights missing from user. learn.microsoft.com/en-us/previous-versions/windows/it-pro/… Commented Jun 6, 2019 at 12:10
12

I simply called WMI from powershell to do this.

$Svc = Get-WmiObject win32_service -filter "name='ServiceName'"
$Svc.Change($Null, $Null, $Null, $Null, $Null, $Null, "User", "Password")

Don't forget to restart the service afterwards:

Stop-Service -Name 'ServiceName'
Start-Service -Name 'ServiceName'

For more fun with WMI and services, see Win32_Service Class

2
  • 14
    You have an odd definition of simple. ;) Commented Nov 11, 2012 at 1:17
  • 3
    It's simple if you understand WMI. Of course, WMI isn't simple. :-) Commented Mar 21, 2016 at 20:40
3

Using WMI results in non-encrypted communication between your machine and the machine you are changing the service credentials on. So your new password can be sniffed quite easily. You just have to parse the WMI blob send over the network. By now I found no really secure way to change a service accounts password remotely with a tool.

1
1

For those who are wondering how to pass a secure password:

$credentials = Get-Credential -UserName 'Domain\username' -Message 'Enter password below'
$service = Get-WmiObject win32_service -filter "name='SERVICE_NAME'"
$service.Change($null,$null,$null,$null,$null,$null,$credentials.username,($credentials.Password | ConvertFrom-SecureString))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.