PowerShell scripts to remotely manage Windows Services

Following on from a previous post I published on how to remotely manage IIS via PowerShell scripts I thought it would be useful to detail how to also manage Windows Services remotely.

RESTART A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME] | Restart-service

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Restart-service
STOP A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME] | Stop-service

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Stop-service
START A WINDOWS SERVICE
Get-Service -Name [INSERT_SERVICE_NAME]  -ComputerName [INSERT_COMPUTER_NAME] | Start-service

Example:

Get-Service -Name Jonathan.Welch.Test.Service  -ComputerName jon-app-01 | Start-service
START A DISABLED WINDOWS SERVICE
Get-WmiObject Win32_Service -ComputerName [INSERT_COMPUTER_NAME] | ? { $_.Name -Match '[INSERT_SERVICE_NAME]' -and $_.StartMode -ne 'Disabled' } | % { $_.StartService() }

Example:

Get-WmiObject Win32_Service -ComputerName jon-app-01 | ? { $_.Name -Match 'Jonathan.Welch.Test.Service' -and $_.StartMode -ne 'Disabled' } | % { $_.StartService() }

Note that in the examples above you can append a * to the end of service name to find services that match that name. So you could shorten the name to Jonathan.Welch* to find all services with names that start with Jonathan.Welch

One thought on “PowerShell scripts to remotely manage Windows Services

Leave a comment