Easiest way to grant "Log on as a service" to a Windows user from the command-line?

I want to script an install where a service needs to be run as a user. I want to be able to specify the user.

Creating the user is easy through the NET USER /ADD command.

Specifying the user for the service can also be done: the SC CONFIG command allows this.

Now the missing link: granting the user the "Log on as a service" privilege as a logon right (SeServiceLogonRight). Is there a command for this? Or a simple script for PowerShell?


The easiest way to do this from a command line is definitely using NTRights.exe from the Windows Server 2003 Resource Toolkit.

ntrights +r SeServiceLogonRight -u jeroen -m \\%COMPUTERNAME%

I use Carbon and Chocolatey.

cinst carbon -y

Grant-Privilege -Identity $username -Privilege SeServiceLogonRight

Based on this answer: https://stackoverflow.com/a/22155390/991267