≡ Menu

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.


(Get-WMIObject -computername remotepc -class win32_service "Name='alerter'").stopservice()

(Get-WMIObject -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.


(Get-WMIObject -computername remotepc -class win32_service "Name='alerter'") | gm

Comments on this entry are closed.

  • ILTim September 8, 2010, 6:02 pm

    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?

  • Sitaram Pamarthi September 8, 2010, 6:30 pm

    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.

  • Hernan Rojas April 15, 2011, 8:16 am

    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.

  • John Taylor June 29, 2011, 6:16 pm

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

  • charlie November 16, 2011, 9:54 pm

    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)

  • kailash January 13, 2014, 10:54 pm

    HI Powershell guys,an very new to powershell – need help please with request : stopping all services in services.txt on all servers in servers.txt.
    what i have sofar is :
    $ServerList = Get-Content “c:\Services2Restart0Servers2Check.txt”
    $ServiceList = Get-Content “c:\Services2Restart1Services2Check.txt”

    foreach ($Server in $ServerList)
    {
    (stop-service -inputobject $(get-service -ComputerName $Server -Name $Service)
    }
    this is my error:
    Stop-Service : Cannot validate argument on parameter ‘InputObject’. The argument is null or empty. Supply an argument that is
    not null or empty and then try the command again.
    At D:\ETSSOData\Intel\Distribution2eTServers\DistrPerfReport\ServicesRestart\ServicesRestart-RemoteMultiple.ps1:7 char:26
    + stop-service -inputobject <<<< $(get-service -ComputerName $Server -Name $Service)
    + CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StopServiceCommand.

    What to do pls help to get me this working, much appraciated