PowerTip: Use PoweShell to Start Service on Remote Machine


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

Comments (21)

  1. Anonymous says:

    And to start a service:

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

  2. jrv says:

    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.

  3. Richard.Siddaway says:

    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

  4. jrv says:

    @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.

  5. Anonymous says:

    You can also this:

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

  6. jrv says:

    @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.

  7. jrv says:

    @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.

  8. jrv says:

    @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.

  9. heyko says:

    It looks like this problem is solved with Windows Server 2012 R2 (and maybe earlier..).

    I’ve tried it between two 2012 R2 machines and was able to stop and start services w/o problems.

  10. Richard.Siddaway says:

    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

  11. jrv says:

    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…

  12. Chen V says:

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

  13. Chen V says:

    set-service servicename -startuptype Auto

    This will ensure its in Automatic mode

  14. xJosh says:

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

  15. Javed says:

    Dear All,

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

    Regards
    Javed

  16. tomm says:

    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)

  17. Tremujin says:

    Command run on SBS 2011 Server PowerShell v2-0-1-1

    Get-Service -Name remoteregistry -ComputerName PC1 | Start-Service

    Starts service on SERVER

    Command run on Server 2012 PowerShell v3-0-1-1

    Get-Service -Name remoteregistry -ComputerName PC1 | Start-Service

    Starts service on PC1

    jrv doesn’t work the same on Powershell v2-0-1-1

    Thanks TomM that command ran perfectly.

  18. Nagaraju V says:

    I use the both, but only
    " Get-Service -Name bits -ComputerName RSLAPTOP01 | Set-Service -Status Running " command works for me.

  19. regina says:

    Written in version 2.0

    $hosts = Import-Csv C:tempHosts.csv

    foreach ($item in $hosts)
    {
    $Hostname = $item.name
    write-host $Hostname
    #Ping host
    $IP = (Test-Connection $hostname -Count 1 -ErrorAction SilentlyContinue|Select-Object -expandproperty protocoladdress)
    if ($IP -eq $null)
    {continue}
    #Get state of service
    $servicestate = Get-Service -ComputerName $Hostname brokeragent -ErrorAction SilentlyContinue
    #If service is stopped or does not exist, continue otherwise set to stopped and disabled
    if ($servicestate.status -eq ‘Stopped’ -or $servicestate.status -eq $null)
    {Continue}Else{
    (Get-Service -ComputerName $Hostname brokeragent).STOP()
    Set-Service -ComputerName $Hostname -Name brokeragent -StartupType Disabled
    Write-Host "stopped services" -BackgroundColor yellow
    Get-Service -ComputerName $Hostname brokeragent
    }
    }

  20. Manish Singh says:

    Hi All,

    In my production environment I am having more then 37000 computers and every day I have to check services status of few computers.

    I am using below command to get current status of services but its not fetching results for all the IP its fetching result for only one comp

    Query which I run
    PS C:\> Get-Service -computerName VAIO , DELL -Name Dhcp |Select Name, MachineName, Status

    output which I got

    Name MachineName Status
    —- ———– ——
    Dhcp VAIO Running

    Need Guidance!!!

Skip to main content