Set-Service : A parameter cannot be found that matches parameter name 'Credential'

Viewed 4186

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.

2 Answers

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.

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"
}
Related