7

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
  • 357
  • 4
  • 16
  • 6
    That’s for version 7. Here is version 5.1 https://docs.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

1 Answers1

0

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
  • 174
  • 10