To find all the cmdlets that are related to Windows services, execute the following command:
Get-Command -CommandType cmdlet *service*
Get-Service
To find all the running services that start with SQL by executing the following command:
Get-Service -DisplayName "SQL*" -Computername RemoteServerName | Where-Object {$_.status -eq "Running"}
To see all the services whose name begins with ‘‘SQL’’ but exclude any service name that has the word ‘‘Writer’’ from the output:
Get-Service -Include "SQL*" -Exclude "*Writer*" -ComputerName RemoteServerName | Where-Object {$_.status -eq "Running"}
To list all the properties or methods associated with the Get-Service cmdlet:
Get-Service | Get-Member -MemberType properties
Get-Service | Get-Member -MemberType methods
To display the status of the Windows service "aspnet_state" and then try to start and then stop the service:
(Get-Service -Name "aspnet_state").status
(Get-Service -Name "aspnet_state").start()
(Get-Service -Name "aspnet_state").status
Start-Sleep 5
(Get-Service -Name "aspnet_state").status
(Get-Service -Name "aspnet_state").stop()
(Get-Service -Name "aspnet_state").status
Start-Sleep 5
(Get-Service -Name "aspnet_state").status
Stop-Service
To show the status of the service, stop the service, and then show the status of the service again:
Get-Service -Name aspnet_state
Stop-Service -Name aspnet_state
Get-Service -Name aspnet_state
To stop all the SQL Server–related services on the local machine and preview the results:
Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Running"} | Stop-Service -WhatIf
To stop all the SQL Server–related services on the local machine one by one with confirmation:
Get-Service -Displayname *sql* | Where-Object {$_.Status -eq "Running"}| Stop-Service -confirm
or Stop-Service -Displayname *sql* -Confirm
Start-Service
To start the SQLAgent$SQL2008 service:
Start-Service -Name SQLAgent`$SQL2008
Get-Service -Name SQLAgent`$SQL2008
to start all the SQL Server–related services that are stopped with a WhatIf or confirm:
Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Stopped"} | Start-Service -WhatIf
Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Stopped"} | Start-Service -confirm
Set-Service
To use Set-Service to change the startup type from Manual to Automatic:
Set-Service -Name "SQLAGENT`$SQL2008" -StartupType "Automatic"
Note that you cannot stop and start a service on a remote machine using Stop-Service and Start-Service yet. The -computername option may become available in the official Windows PowerShell 2.0 RTM release.