2

What's equivalent SC command for below powershell script ?

Get-Service -ComputerName localhost -Name Spooler | select machinename,name,starttype,status
2
  • Get together output from sc.exe query Spooler, sc.exe queryex Spooler and sc.exe qc Spooler. Commented Jul 1, 2021 at 15:28
  • @JosefZ can you give me sample script? thanks Commented Jul 1, 2021 at 17:45

2 Answers 2

2

Use the qc command to query the service config for the startup type and name:

~> sc.exe qc spooler

SERVICE_NAME: spooler
        TYPE               : 110  WIN32_OWN_PROCESS (interactive)
        START_TYPE         : 4   DISABLED
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : C:\WINDOWS\System32\spoolsv.exe
        LOAD_ORDER_GROUP   : SpoolerGroup
        TAG                : 0
        DISPLAY_NAME       : Print Spooler
        DEPENDENCIES       : RPCSS
                           : http
        SERVICE_START_NAME : LocalSystem

Use the query or queryex commands to fetch the status:

~> sc.exe query spooler

SERVICE_NAME: spooler
        TYPE               : 110  WIN32_OWN_PROCESS  (interactive)
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 1077  (0x435)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
Sign up to request clarification or add additional context in comments.

2 Comments

so how can I get similar result to the output of powershell script ? machinename,name,starttype,status
@Arbelac By running the PowerShell script. Why are you so intent on recreating the output you already have with sc.exe?
0

Since you tagged only powershell one must assume you're running sc.exe in powershell. I would like to understand why you aren't just using Get-Service? In case you have a real need/valid reason for it, you can try this function.

Function Get-SC {
    [cmdletbinding()]
    Param(
        [parameter(Position=0)]
        $Service,
    
        [parameter(Position=1)]
        $Computer = 'Localhost'
    )

    $StartTypeLookup = @{
        AUTO_START   = 'Automatic'
        DEMAND_START = 'Manual'
        DISABLED     = 'Disabled'
    }

    switch -Regex (sc.exe \\$computer qc $service){
        'DISPLAY_NAME\s+:\s(.+)' {$name = $matches.1}
        'START_TYPE[\s\d:]+\s+(\w+)' {$starttype = $StartTypeLookup[$matches.1]}
    }

    $state = switch -Regex (sc.exe \\$computer query $service){
        'STATE\s+.+?(\w+)\s*$' {$matches.1}
    }

    [PSCustomObject]@{
        MachineName = $computer
        DisplayName = $name
        Name        = $Service
        StartType   = $starttype
        Status      = $state
    }
}

Some testing and results

Example 1

Get-SC WpcMonSvc

MachineName : Localhost
DisplayName : Parental Controls
Name        : WpcMonSvc
StartType   : Automatic
Status      : STOPPED

Example 2

Get-SC -Computer kids1 -Service spooler

MachineName : kids1
DisplayName : Print Spooler
Name        : spooler
StartType   : Automatic
Status      : RUNNING

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.