0

I'm trying to start multiple services using :

Get-Service SERVICE* | Start-Service

When Get-Service returns services that are disabled, I run into the following error :

    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
   ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

How do I filter out the services that are disabled ?

1 Answer 1

2

You can use the following code. This will stop all started services, and then start all services, even if they hadn't been started before, but are not disabled either:

get-service SERVICE* | where-object {$_.Status -eq "Running"} `
                     | stop-service

get-service SERVICE* | where-object {$_.StartType -ne "Disabled" `
                            -and     $_.Status -eq "Stopped"} `
                     | start-service

You can simplify this if all services are supposed to be started if not disabled by using this code:

get-service SERVICE* | restart-service

3
  • I suppose I can apply the same principle to the stop operation by doing something like : get-service SERVICE* | where-object {$_.StartType -ne "Disabled" -and $_.Status -eq "Running"} | stop-service`
    – Sybuser
    Commented Sep 13, 2022 at 9:30
  • Yes, definitely. :) Although in that case, you do not have to filter out disabled services, as a disabled service can't be started, so it won't be running anyway.
    – LPChip
    Commented Sep 13, 2022 at 9:30
  • You can of course also use restart-service ;)
    – LPChip
    Commented Sep 13, 2022 at 9:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .