Start/Stop/Restart service on remote computer with powershell

by Sitaram Pamarthi on August 17, 2010

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

This post was written by...

– who has written 493 posts on Techibee.com.

Sitaram Pamarthi is a windows administrators with 9+ years of experience.read more

{ 7 comments… read them below or add one }

ILTim September 8, 2010 at 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?

Reply

Sitaram Pamarthi September 8, 2010 at 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.

Reply

Hernan Rojas April 15, 2011 at 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.

Reply

John Taylor June 29, 2011 at 6:16 pm

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

Reply

charlie November 16, 2011 at 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)

Reply

Sitaram Pamarthi November 17, 2011 at 7:09 pm

Hi Charlie,

The question you asked is most common requirement. So, I authored a separate post for that. See http://techibee.com/powershell/powershell-start-and-stop-services-on-remote-computer-with-alternate-credentials/1171

Hope this helps.

Reply

kailash January 13, 2014 at 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

Reply

Leave a Comment

{ 3 trackbacks }

Previous post:

Next post: