Home > PowerShell > Start/Stop/Restart service on remote computer with powershell

Start/Stop/Restart service on remote computer with powershell

Today I got a requirement to restart a service on remote computer. I thought of doing it with powershell using restart-service powershell cmdlet but it disappointed me. Though Get-Service cmdlet is offering -computername parameter, stop-service, start-service, and restart-service cmdlets are not offering this feature. That means you can query service status on remote PC but cannot start/stop with built-in facilities/cmdlets.

However, there is a way to do it using WMI in combination with powershell.

(gwmi -computername remotepc -class win32_service “Name=’alerter’”).stopservice()

(gwmi -computername remotepc -class win32_service “Name=’alerter’”).startservice()

There is no direct method available to restart a service directly, but you can achieve similar functionality with stop followed by start method which makes sense.

You like to know what more we can do here? Just issue below command.

(gwmi -computername remotepc -class win32_service “Name=’alerter’”) | gm

  1. ILTim
    September 8, 2010 at 6:02 pm | #1

    I found your post and also the following on microsofts site while researching the same problem:

    Invoke-Command -ComputerName Server01 {Restart-Service Spooler}

    http://technet.microsoft.com/en-us/library/dd315239.aspx

    Seems like a more direct method?

  2. September 8, 2010 at 6:30 pm | #2

    Agree.

    But the MS one works only when you have remoting(new with powershell v2) enabled on remote computer. The command I provided helps in both conditions(with or without remoting) as this purely depends on WMI which is available XP/Vista/Windows 7.

  3. Hernan Rojas
    April 15, 2011 at 8:16 am | #3

    I tried the folowwing sentence and it worked for me!!
    Stop-Service -InputObject (get-Service -ComputerName server -Name spooler)

    source computer running Powershell v2, destination computer Win2K3 R2 with no PS installed.

    Hernan.

  4. John Taylor
    June 29, 2011 at 6:16 pm | #4

    This worked for me:
    (gwmi -computername $server -class win32_service | Where-Object { $_.Name
    -eq “$spooler” }).startservice()

  5. charlie
    November 16, 2011 at 9:54 pm | #5

    I can use the following and it works for me perfect, atleast for machines in my domain. i’m trying to use credentials and run this on systems in another domain. Any suggestions?

    Get-credential domain\username
    Stop-Service -InputObject (get-Service -ComputerName $server -Name spooler) -force
    start-sleep 10
    Start-Service -InputObject (get-Service -ComputerName $server -Name spooler)

  1. November 17, 2011 at 7:07 pm | #1
  2. November 22, 2011 at 7:45 pm | #2

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>