How to create a Windows service account programmatically

2.1k views Asked by At

I need to programmatically create a new Windows account for running a Windows service I recently developed.

Due to lack of sufficient privileges I cannot use any of the existing service accounts (LocalService, NetworkService and LocalSystem), so I have to create my own account during installation of my service.

Unfortunately, I have no idea on how to accomplish this from code (C#). However, I know that the steps I have to go through include:

  • Create the account
  • Deny account log on via console
  • Grant log on as a service.
  • Add the account to the local administrators group on the PC

My service must install and run on all Windows PC operating systems ranging from Windows XP SP3 and up.

Question: Which command line tools are available for this purpose (I can very well call those command line tools from code)?

Further, any relevant links, code snippets or scripts will be very much appreciated!

1

There are 1 answers

2
leonigmig On

If you just want a single command you can probably do:

net user /ADD "newuser" "Pass phrase" /passwordchg:no

However I looked into 'grant logon as a service' in the past, and had to download an additional .exe from a Windows Server Resource Pack to do this. That may be a pain as you'll need to redistribute the .exe.

Unless someone suggests a better way which uses only built in .exes I'd try and do this with one of:

  • Powershell
  • VBscript