Powershell New-Service With User Credentials

Solution 1:

It needs an object of type PSCredential. It can be created manually using the command Get-Credential.

There are other ways, but they are a bit tricky because the password needs to be passed in as a SecureString.

You can find how to programmatically build a PSCredential object here.


Sample code:

$username = "username"
$password = "password"
$securepassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $securepassword)

New-Service [...] -Credential $cred