fbpx

Using Powershell start-service And Stop Service In Windows 10 | 2 Best Ways

Services are programs that run in the background of Windows 10. There may be occasions when you need to manage them manually because a feature or application isn’t functioning properly or demands it.

Using Powershell start-service And Stop Service In Windows 10

For each of the selected services, the Start-Service cmdlet sends a start message to the Windows Service Controller. The notification is automatically ignored if a service is already active. The start-service and *-Service PowerShell cmdlets, as well as how to utilize them, are all covered in this lesson. You’ll also learn how to create your own script to manage services on numerous computers at once.

Using Get-Service in PowerShell to List Services

Enumerating the services that are present on a local machine is one of the simplest activities you can carry out using PowerShell and Windows services. 

Launch PowerShell, perform Get-Service, and review the results.

Get-Service,

By itself, Get-Service will list every service on the local computer along with its Status, Name, and DisplayName.

Get-Service,

PowerShell does not, however, return all of the properties for each service, as many other cmdlets do. If you’d want to view a service’s necessary services or possibly the service description, you can do so by piping the output to Select-Object and using the * symbol to denote every property.

Where Can I Find Remote Services?

Perhaps you need to enumerate services across one or more distant Windows systems since you’re connected to a network. This might have been accomplished in the past using the ComputerName argument in Windows PowerShell, but sadly, it is no longer available.

However, PowerShell Core still makes it possible to locate remote services using two alternative approaches: PowerShell Remoting and CIM/WMI.

1. Get-Service and PowerShell Remoting

Using PowerShell Remoting (PS Remoting) is one method for remotely inspecting Windows services. You can encapsulate any local command and execute it in a remote session the same way you would locally by utilizing PS Remoting.

If a remote machine has PowerShell Remoting enabled, you might use Invoke-Command to execute Get-Service as shown below.

Keep in mind that in an Active Directory (AD) environment, the Credential option is not required.

$cred = Get-Credential
Invoke-Command -ComputerName SRV1 -ScriptBlock { Get-Service } -Credential $
$cred = Get-Credential
Invoke-Command -ComputerName SRV1 -ScriptBlock { Get-Service } -Credential $

When Invoke-Command is executed, all of the data supplied by Get-Service is sent, and you will receive the services you requested.

Be aware of the additional PSComputerName property. Invoke-Command returns this property. A straightforward script can be written to enumerate services on numerous remote PCs as well.

$cred = Get-Credential
 $computers = Get-Content -Path 'C:\computers.txt'
 foreach ($name in $computers) {
     $services = Invoke-Command -ComputerName $name -Credential $cred -ScriptBlock {Get-Service}
     [pscustomobject]@{
         ComputerName = $name
         Services = $services
     }
 }
Get-Service and PowerShell Remoting

2. Finding Services with CIM/WMI

It might not be appropriate to use Get-Service with PowerShell. Instead, you can use a CIM session to query CIM/WMI. You can use a CIM session without needing to use PowerShell Remoting.

You can use CIM to locate and manage services by:

The creation of a PSCredential object. 
Since neither of the two PCs is located in an AD environment, we must use the Credential option. Create a CIM session by supplying the computer’s name and the authentication credential.

To make a query to the Win32_Service class, use Get-CimInstance.

$serverName = 'SRV1'
$cred = Get-Credential
$cimSession = New-CimSession -ComputerName $serverName -Credential $cred
Get-CimInstance -CimSession $cimSession -ClassName Win32_Service
Finding Services with CIM/WMI

When finished, don’t forget to delete the CIM session.

Remove-CimSession -CimSession $cimSession
image 35

Windows PowerShell Start-Service and Stop-Service Cmdlet

PowerShell also allows you to start and stop services. There are a few ways to start a service.

1. Using PowerShell Stop-Service And Start-Service Command

The Start-Service and Stop-Service cmdlets are the name of the service. These cmdlets carry out your expectations exactly. Both the pipeline and the Name parameter can be used to use them.

$serviceName = 'wuauserv'
Stop-Service -Name $serviceName
## Stop a service with the pipeline
Get-Service $wuauserv | Stop-Service

You can tab-complete service name values using the Name and Display Name arguments using any of the *-Service cmdlets. Simply enter -Name, a space, and then start pressing the Tab key. You’ll observe that it cycles through all of the local computer’s services.

The same idea also applies to start the service.

## Stop a service with the Name parameter
$serviceName = 'wuauserv'
Start-Service -Name $serviceName
## Stop a service with the pipeline
Get-Service $wuauserv | Start-Service

The Stop-Service and Start-Service cmdlets are both idempotents, which means that if you try to stop or start the service that already running or stopped, the cmdlets will simply skip over the service.

Once more, you must enclose these commands in a scriptblock and use PowerShell Remoting to execute them remotely in order to start and stop remote services using PowerShell.

$cred = Get-Credential
$serviceName = 'wuauserv'
Invoke-Command -ComputerName SRV1 -ScriptBlock { Start-Service -Name $using:serviceName } -Credential $cred

2. Starting/Stopping Remote Services with PowerShell

Using similar code to that used to enumerate services, you can also extend it to distant machines. Remember that you can always wrap any code in a scriptblock and run it from a distance using PowerShell Remoting. 

You can utilize a CIM session to remotely start and stop services. The CIM session you made could be recycled.

$serverName = 'SRV1'
$cred = Get-Credential
$cimSession = New-CimSession -ComputerName $serverName -Credential $cred

Use the Invoke-CimMethod cmdlet after creating a CIM session, but don’t forget to delete the CIM session when you’re through.

Get-CimInstance -CimSession $cimSession -ClassName Win32_Service -Filter "StartMode='Auto' And State='Stopped'" | Invoke-CimMethod -MethodName StartService
Remove-CimSession -CimSession $cimSession
Remove-CimSession -CimSession $cimSession

Using PowerShell To Restart a Service

A service that has already started may need to be restarted. PowerShell doesn’t have a problem with that. Once more, you have two options.

1. Use Start and Stop Service

The Stop-Service and Start-Service cmdlets could be used to simply stop and start the service.

## Restart a service with the Name parameter
$serviceName = 'wuauserv'
Stop-Service -Name $serviceName
Start-Service -Name $serviceName
## Restart a service with the pipeline and PassThru parameter
$serviceName = 'wuauserv'
Stop-Service -Name $serviceName -Passthru | Start-Service

2. Using the Restart-Service cmdlet in PowerShell

You should use the Restart-Service cmdlet if you want to keep the PowerShell code for restarting a service to a minimum. This command executes exactly as you would expect and functions similarly to other service commands.


The result of Get-Service can be piped directly into Restart-Service as shown below to start the wuauserv and save some code.

## Restart a service with the Name parameter

$serviceName = 'wuauserv'
Get-Service -Name $serviceName | Restart-Service
Get-Service -Name $serviceName | Restart-Service

3. Changing the Startup Type

Changing the startup type for services is another common activity. The characteristic that determines what the services perform when Windows starts up is the startup type. 

  • Automatic: When Windows starts, the service also does.
  • Disabled: The service won’t ever launch.
  • Manual: The service can be started, but it must be done so.
  • Automate – Delayed: As soon as Windows begins, the service launches automatically but with a delay.

Let’s assume that your initial requirement is to understand the startup type of a service. This can be located using Get-Service.

The startup type is referred to by Get-Service as Status and is displayed as the Status property if you’re using Get-Service to discover it.

(Get-Service -Name wuauserv).StartupType

Using Group-Object, you can immediately see the startuptype values for each service. When you are aware of the existing startup type, you can modify it with Set-Service.

Set-Service -Name -StartupType Disabled