# Start Automatic Services on Remote machines if stopped

**URL:** <https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890>\
**Category:** PowerShell Help\
**Created:** [September 18, 2013, 12:43am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890 "2013-09-18T00:43:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![suryakanta-das](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/suryakanta-das/32/939_2.png) [@suryakanta-das](https://forums.powershell.org/u/suryakanta-das)\
**Post date:** [September 18, 2013, 12:43am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/1 "2013-09-18T00:43:41Z")

</div>

Hi Guys,

I am trying to create a script here which will find the Stopped “Automatic” services on the servers, as per “C:\ServerList.txt” and will Start them.

With the below command:

Get-wmiobject win32\_service -Filter “startmode = ‘auto’ and state != ‘running’” -computername (Get-Content C:\ServerList.txt) | Select-Object -ExpandProperty Name

I get the below output:

CTXCPUBal  
ShellHWDetection  
ShellHWDetection  
ShellHWDetection  
stisvc

But when I put “Start-Service” in the pipeline after the whole command,

Get-wmiobject win32\_service -Filter “startmode = ‘auto’ and state != ‘running’” -computername (Get-Content C:\ServerList.txt) | Select-Object -ExpandProperty Name | Start-Service

I get the below error:

Start-Service : Cannot find any service with service name ‘stisvc’.  
At line:1 char:203

- Get-wmiobject win32\_service -Filter “startmode = ‘auto’ and state != ‘running’” -computername (Ge  
t-Content C:\UTILS\FarmChecks\Uptime\ServerList.txt) | Select-Object -ExpandProperty Name | Start-S  
ervice \<\<\<\<
  - CategoryInfo : ObjectNotFound: (stisvc:String) [Start-Service], ServiceCommandExcep  
tion
  - FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StartServic  
eCommand

And after that, when I type the first command to find the Automatic services those are stopped, as per the C:\ServerList.txt, the output remains the same:

CTXCPUBal  
ShellHWDetection  
ShellHWDetection  
ShellHWDetection  
stisvc

Could any please tell me, where I am doing wrong in the above command. I am unable to get answers for below things to be honest:

1. When we are passing objects as Strings and “Start-Service” does take object Strings, why this is not working?

2. If it’s working for rest of the services, as it was throwing error for “stisvc” only, the rest of the services shouldn’t come up again, when I run the first command. That means it’s not working for the rest either.

3. Checked on the set of servers, which are in the C:\ServerList.txt file, but couldn;t find the services started, but it’s not throwing any errors either. Further on found all those services exist on Local machine, from where I am trying to run the above command. Is it trying to start services locally? But why?

Any help would be much appreciated.

Kind Regards,  
Surya

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [September 18, 2013, 12:53am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/2 "2013-09-18T00:53:48Z")

</div>

start-service only works on the local server

you will need to rewrite your script so that you test the service and use the StartService method of Win32\_Service to start the service

---

<div class="post-metadata">

**Author:** ![suryakanta-das](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/suryakanta-das/32/939_2.png) [@suryakanta-das](https://forums.powershell.org/u/suryakanta-das)\
**Post date:** [September 18, 2013, 3:05am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/3 "2013-09-18T03:05:17Z")

</div>

Hi Richard,

Though I understood that you are asking me to use “StartService” Method, which I can get when I pipe “Get-WmiObject Win32\_Service” to Get-Member, but am unable to figure out how to do that.

Could you please help me with that.

Cheers!  
Surya

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 18, 2013, 3:13am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/4 "2013-09-18T03:13:26Z")

</div>

Give this a try:

```
Get-WmiObject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) |
ForEach-Object {
    $result = $_.StartService()
    # You can check the value of $result.ReturnValue here; if it's zero, the service started successfully.
}
```

---

<div class="post-metadata">

**Author:** ![suryakanta-das](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/suryakanta-das/32/939_2.png) [@suryakanta-das](https://forums.powershell.org/u/suryakanta-das)\
**Post date:** [September 18, 2013, 3:51am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/5 "2013-09-18T03:51:32Z")

</div>

Brilliant One Dave! Thank You very much! It really made the one liner script work, but now I have a new problem…

After doing the one-liner script, though it started the services on remote computers, but when I tried to retrieve the data whether it worked or not, through the next line command

“Get-WmiObject win32\_service -Filter “startmode = ‘auto’ and state != ‘running’” -computername (Get-Content C:\ServerList.txt) | Select-Object Name, State, Status”

(Believe me, I again couldn’t figure out, how to check the value of $result.Returnvalue as advised by you above… Still learning Powershell 🙂 )

I got list of services like the below:

Name Status State

* * *

clr\_optimization\_v4.0.30319\_32 OK Stopped  
clr\_optimization\_v4.0.30319\_64 OK Stopped  
gupdate OK Stopped  
CTXCPUBal OK Stopped  
gupdate OK Stopped  
clr\_optimization\_v4.0.30319\_32 OK Stopped  
clr\_optimization\_v4.0.30319\_64 OK Stopped

Though they might have started and stopped, but in the list of 20 servers, how to find out on which server it really didn’t start…

Is there anyway to get the respective Server Name along with the above output?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 18, 2013, 4:20am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/6 "2013-09-18T04:20:56Z")

</div>

Here’s a fairly straightforward way of outputting the errors if the StartService method failed. It doesn’t take any action, just displays the error on screen. The WMI methods return Win32 error codes, so you can translate them into useful error messages via the Win32Exception class.

```
Get-WmiObject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) |
ForEach-Object {
    $result = $_.StartService()
    if ($result.ReturnValue -ne 0)
    {
        $exception = New-Object System.ComponentModel.Win32Exception([int]$result.ReturnValue)
        Write-Host -ForegroundColor Red "Server: $($_.__SERVER), Error code: $($result.ReturnValue), Message: $($exception.Message)"
    }
}
```

---

<div class="post-metadata">

**Author:** ![suryakanta-das](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/suryakanta-das/32/939_2.png) [@suryakanta-das](https://forums.powershell.org/u/suryakanta-das)\
**Post date:** [September 18, 2013, 4:23am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/7 "2013-09-18T04:23:16Z")

</div>

In addition to that, another query is “Start-Service” doesn’t mention anywhere that it doesn’t work on remote computers and only works on local server… have gone through the “Get-Help Start-Service -Full” command, but nowhere it’s mentioned…

But couldn’t find any parameter as -ComputerName in the syntax, which I assumed that the key to understand that the command works only on local machine, but am I correct in assuming so, or is there any other way available in Powershell??

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 18, 2013, 4:34am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/8 "2013-09-18T04:34:10Z")

</div>

I’ll test this when I’m at home tonight, but I suspect that you actually can use Start-Service on remote computers. You’d just need to pipe it objects obtained from Get-Service, not from Get-WmiObject.

That being said, I would expect the WMI version you’re already using to give better performance. When you’re using Get-Service, you’d have to retrieve records for every service on the remote machines, then use Where-Object to filter them based on start type and status. Using Get-WmiObject’s -Filter parameter does that filtering on the remote computer, and only sends you the data you really need.

---

<div class="post-metadata">

**Author:** ![suryakanta-das](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/suryakanta-das/32/939_2.png) [@suryakanta-das](https://forums.powershell.org/u/suryakanta-das)\
**Post date:** [September 18, 2013, 4:43am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/9 "2013-09-18T04:43:44Z")

</div>

But isn’t it correct that … to get details about “Automatic” services, we have to use “WMI-Object”, as Get-Service can only provide “Name” , “Status” & “DisplayName” properties for a service, but not “Statup Type” or “Executable path” or “LogonAs” properties?

That’s the reason why I had to choose Wmi-Object actually… as first I tried a lot to get the data through Get-Service, as my initial assumption was… Get-Service will get me everything related to Service…

Please correct me if I am wrong…

And really Thank You for helping me out and guiding me till now… Much appreciate it and you guys are the reason why we are able to learn Powershell… Thank You Very Much!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 18, 2013, 4:48am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/10 "2013-09-18T04:48:38Z")

</div>

I hadn’t realized that before, but it looks like you’re right. The .NET ServiceController class (which is what Get-Service and Start-Service use) doesn’t provide any information about the startup type for some reason. With that in mind, just stick with WMI. 🙂

---

<div class="post-metadata">

**Author:** ![suryakanta-das](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/suryakanta-das/32/939_2.png) [@suryakanta-das](https://forums.powershell.org/u/suryakanta-das)\
**Post date:** [September 18, 2013, 5:08am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/11 "2013-09-18T05:08:24Z")

</div>

Sure… will do that… 🙂

Thank You Again!

---

<div class="post-metadata">

**Author:** ![maria-pradeep](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@maria-pradeep](https://forums.powershell.org/u/maria-pradeep)\
**Post date:** [August 3, 2016, 2:14am UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/12 "2016-08-03T02:14:56Z")

</div>

Hello Suryakanta,

Do you have a working script model for the same ? As i am looking for something similar to this .

---

<div class="post-metadata">

**Author:** ![paul-frankovich](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@paul-frankovich](https://forums.powershell.org/u/paul-frankovich)\
**Post date:** [August 5, 2016, 12:54pm UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/13 "2016-08-05T12:54:51Z")

</div>

While this doesn’t answer the start-up type. I have used this successfully in the past to start remote services.

```
Start-Service -InputObject $(Get-Service -ComputerName Server1 -Name Spooler)
Try
{
    $(Get-Service -ComputerName Server1 -Name Spooler).WaitForStatus('Running','00:02:00')
}
Catch
{
    Write-Warning "Service didn't start after two minutes"
}
```

Edit:

I just tried this in my test lab and it failed. Maybe it doesn’t work as well as I thought it did.

---

<div class="post-metadata">

**Author:** ![paul-frankovich](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@paul-frankovich](https://forums.powershell.org/u/paul-frankovich)\
**Post date:** [August 5, 2016, 1:14pm UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/14 "2016-08-05T13:14:27Z")

</div>

It appears to work with some services and not others. Something could be stepping on it when it tries to start the Spooler since it’s set to disabled on the server I tried it on.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/start-automatic-services-on-remote-machines-if-stopped/1890/15 "2024-05-16T20:40:45Z")

</div>


