PowerTip: Use PoweShell to Start Service on Remote Machine

PowerTip: Use PoweShell to Start Service on Remote Machine

  • Comments 16
  • Likes

Summary: Learn how to use Windows PowerShell to start a service on a remote machine.

Hey, Scripting Guy! Question How can I start a service on a remote machine?

Hey, Scripting Guy! Answer To start a service on a local machine:

Get-Service -Name bits  | Start-service

But if you try to use the same technique on a remote machine, it tries to start the service on your local machine:

Get-Service -Name bits  -ComputerName RSLAPTOP01 | Start-service

So you have to use Set-Service:

Get-Service -Name bits -ComputerName RSLAPTOP01 | Set-Service -Status Running

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I use this:

    Get-Service -Name bits  -ComputerName RSLAPTOP01 | Start-service

    On my system (Windows 7 PS 3) to XP machine and it works correctly.  I am not sure why you think it won't work:

    Here is the proof:

    PS C:\scripts> get-service bits

    Status   Name               DisplayName

    ------   ----               -----------

    Running  bits               Background Intelligent Transfer Ser...

    PS C:\scripts> get-service bits -computer devws2

    Status   Name               DisplayName

    ------   ----               -----------

    Running  bits               Background Intelligent Transfer Ser...

    PS C:\scripts> get-service bits -computer devws2 |stop-service

    PS C:\scripts> get-service bits -computer devws2

    Status   Name               DisplayName

    ------   ----               -----------

    Stopped  bits               Background Intelligent Transfer Ser...

    PS C:\scripts> get-service bits

    Status   Name               DisplayName

    ------   ----               -----------

    Running  bits               Background Intelligent Transfer Ser...

    PS C:\scripts> get-service bits -computer devws2 |start-service

    PS C:\scripts> get-service bits -computer devws2

    Status   Name               DisplayName

    ------   ----               -----------

    Running  bits               Background Intelligent Transfer Ser...

  • I suggest that you look at the help documentation for the service cmdlets. Start-Service doesn't have a computer name parameter and has no way to access remote machines.

    The service name is passed into the start-service cmdlet and attempts to start a local service based on the name

  • @Richard - It works if you retrieve the remote object with Get-Service.  If you look at the post It shows that the remote service is stopping and starting.  It works fine. Try it.

    In PowerShell V2 and earlier it did not work. V3 seems to have been changed.  Never completely trust PowerShell documentation.

  • Interesting thing I just discovered is that it does not work on Windows 8.  I winder if Windows 7 has some settings that are different or maybe I am hallucinating.

  • @Richard - I was typing the wrong thing on Windows 8 - wrong machine ID.

    On both Windows 8 and 7 Get-Service | Stop-Service  works as expected.  It works to XP and Windows 7.  I don't have all of the firewall ports open on the Windows 8 laptop so I didn't test it.  The management ports need to be open.

    I would say that any machine we can do a Get-Service on remotely will work.  This is as it is with SC which uses the same API.  If I have a handle to an open service object I can send all API commands.  While it would be more convenient at times to have a computer parameter on Stop/Start service CmdLets it is not needed.

    Go ahead and test it.

  • I've done some further testing - my post is true for PowerShell 2.  The problem seems to have been resolved in PowerShell 3 which I hadn't picked up on.  Apologies for the confusion

  • @Richard - I just tested with PowerShell V2 and it fails as expected with this error:

    PS C:\scripts> Get-service bits -ComputerName omega | stop-service

    stop-service : Service 'Background Intelligent Transfer Service (bits)' cannot be stopped due to the following error: C

    annot open bits service on computer '.'.

    At line:1 char:50

    The exact same line of code works with no error in V3.  I believe it also requires all patches to Net 4.0 to work correctly.

  • @Richard - no problem.  I was confused too the first time someone posted that it worked.  I had to repeat the test many time before I was convinced.

    Keep posting those great blogs.

  • Good to know Set-Service for remote machine!!!

  • set-service servicename -startuptype Auto

    This will ensure its in Automatic mode

  • I was able to do this with the following set-service -computername -name -status running Hopefully that isn't bad practice...

  • Dear All,

    I want to start the services of a server from local desktop can any one will help me

    Regards
    Javed

  • Try this it works for me.

    COMPUTER1 is the name of the remote machine. spooler is just an example service.

    Start a service on remote computer:

    Start-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

    Stop a service on remote computer:

    Stop-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

    Restart a service on remote computer:

    Restart-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

  • You can also this:

    (Get-Service -ComputerName RSServer -Name "Service Name").Stop()

  • And to start a service:

    (Get-Service -ComputerName RSServer -Name "Service Name").Start()