35

I want to invoke sc create from a powershell script. Here is the code.

function Execute-Command
{
    param([string]$Command, [switch]$ShowOutput=$True)
    echo $Command
    if ($ShowOutput) {
        Invoke-Expression $Command
    } else {
        $out = Invoke-Expression $Command
    }
}

$cmd="sc create `"$ServiceName`" binpath=`"$TargetPath`" displayname=`"$DisplayName`" "
Execute-Command -Command:$cmd

which gives the following error:

Set-Content : A positional parameter cannot be found that accepts argument 'binpath=...'.
At line:1 char:1

What is the problem? What are positional arguments?

3

3 Answers 3

82

The issue here is not with the sc executable. As the error states, sc resolves to Set-Content. If you issue Get-Alias -Name sc, you'll see:

gal sc

To bypass the alias, use the full name of the executable (including the file extension):

PS C:\> sc.exe query wuauserv

SERVICE_NAME: wuauserv
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

You might want to use the -f operator when constructing your command line arguments, to avoid those annoying quote-escaping back ticks all over the place:

$CmdLine = 'sc.exe create "{0}" binpath= "{1}" displayname= "{2}" ' -f $ServiceName,$TargetPath,$DisplayName
Execute-Command -Command $CmdLine
4
  • 8
    Why Microsoft? Why?
    – Alvaro
    Commented Oct 2, 2020 at 22:53
  • @Alvaro Not sure I understand the question? Commented Oct 3, 2020 at 13:51
  • I'll spell it out for you: why would they create a built-in commandlet alias with a name that conflicts with a commonly used utility that ships with Windows? It seems like a recipe for confusion and annoyance.
    – craig65535
    Commented Mar 2, 2023 at 0:49
  • Same issue, it wastes me half of a day!
    – krizex
    Commented Aug 16 at 8:51
2

The exact command worked for me from a simple Command Prompt console while it fails on PowerShell and VS Code Integrated Terminal. In fact, all sc commands had to be run from Command Prompt as Administrator.

sc create MyService binPath= "C:\svc\sampleapp.exe"
sc start MyService
0

I know Mathias's script works fine, but there is a reason that it would work and the one posted by Gyozo did not. It has to do with a gotcha in sc.exe.

The space after the equals in binPath and displayName are NOT optional.

For me this snippet would not work:

$cmd="sc create `"$ServiceName`" binpath=`"$TargetPath`" displayname=`"$DisplayName`" "

However this one would:

$cmd="sc create `"$ServiceName`" binpath= `"$TargetPath`" displayname= `"$DisplayName`" "

This issue continues to rise up every once in a while to vex me until I remember this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.