8

I am trying to use a powershell script to change the login credential of a service. As per this Microsoft documentation (example 8), I am using the following code:

$credential = Get-Credential
Set-Service -Name serviceName -Credential $credential

When I run the script, I am prompted for a username and password, which I enter, but then the following error results:

Set-Service : A parameter cannot be found that matches parameter name 'Credential'.
At line:2 char:28
+ Set-Service -Name serviceName -Credential $credential
+                            ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Service], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetServiceCommand

What might be causing this? This machine is not on a domain, but I am logged on as a local administrator, and running powershell as an administrator. I have permissions to change these services and can enter the login details normally via the usual Services GUI.

ASForrest
  • 377
  • 6
  • 19
  • 6
    That’s for version 7. Here is version 5.1 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-service?view=powershell-5.1#examples – Doug Maurer Sep 25 '20 at 05:10
  • Thanks, that explains that. I'll ask a new question to try and find another solution. – ASForrest Sep 25 '20 at 05:30
  • 1
    the question was already answered: https://stackoverflow.com/questions/966389/how-to-change-user-credentials-of-windows-service-from-command-line – Guenther Schmitz Sep 25 '20 at 05:55
  • Thanks, I found that one and ended up going that way. I would prefer to use the ps method because it properly checks the credentials as opposed to just blindly poking them in there and assuming the password is correct but if ps won't do the job I guess beggars can't be choosers. – ASForrest Sep 25 '20 at 06:05
  • Hey @ASForrest, have you found a better soluton rather than use `sc.exe` to modify the service logon credentials? – Mr.KoopaKiller Aug 11 '21 at 10:09
  • @Mr.KoopaKiller no, that's how I ended up leaving it. If you find a better way I'm extremely interested to learn about it! – ASForrest Aug 12 '21 at 22:48

2 Answers2

3

The Credential parameter only exist for PowerShell version 6+ (which is out of support).
Probably you are using a version from before PowerShell 6 (most probably 5.1 which is still the main version in Windows 10 and 11).

See below Set-Service in:

Run $PSVersionTable.PSVersion, to your PowerShell version.

If you run (Get-Help -Name Set-Service).Parameters.parameter.Name, you'll find all the available Parameters in the active version.
If you're running 5.1 or older, the Get-Help -Name Set-Service -Parameter Credential should return the error "No parameter matches criteria.".

Installing or updating to PowerShell 7.X is as easy as running winget install Microsoft.PowerShell.

IT M
  • 359
  • 1
  • 2
  • 12
2

Why don't you want to use:

sc.exe config "[servicename]" obj= "[.\username]" password= "[password]"

If you want the user to enter a username/password each time, then you can use:

$cred = Get-Credential
$username = $cred.username
$password = $cred.GetNetworkCredential().password
&sc.exe config "[servicename]" obj= "$username" password= "$password"

if you need to check that the username and password are valid, then you can use:

$currentdomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($currentdomain,$username,$password)

if ($domain.name -eq $null) {
 write-host "Authentication failed - please verify your username and password."
 exit
}
else {
 write-host "Successfully authenticated with domain $domain.name"
}
Re1ter
  • 87
  • 1
  • 9
  • Because it doesn't check that the username and password are correct, so if the user fat fingers the password you won't know until something doesn't work later. – ASForrest Aug 21 '22 at 23:03
  • I think I answered all your questions – Re1ter Aug 31 '22 at 06:24
  • Thanks - I'm currently in the middle of other projects but will probably have a chance to revisit this one in a month or two. I'll give it a try then and see how it goes. Does the above require the PC to be on a domain? These ones typically are not. – ASForrest Sep 01 '22 at 09:25
  • there shouldn't be any restrictions – Re1ter Sep 02 '22 at 16:22