How to Manage Windows Services with PowerShell

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

There are many ways to manage Windows Services. This article will cover the best practices for managing services with PowerShell in a consistent, repeatable and scalable way.

The “get-service powershell” is a command that allows users to manage Windows services with PowerShell. The command will return all of the running services for the current user.

How to Manage Windows Services with PowerShell

Windows services is a problem that almost every Windows administration must deal with. You may use the services to manage Windows services. msc MMC snap-ins are great for one-off activities, but what if you need to automate something using PowerShell? In this article, you’ll learn how to obtain a service, start a service, stop a service, and restart a service using PowerShell.

It’s time to learn how to use PowerShell to manage services.

In this article, you’ll learn how to utilize the *-Service PowerShell cmdlets and create your own script to manage services on several PCs at once.

Prerequisites

This post will take you through a step-by-step tutorial of how PowerShell can read and control Windows services. Please make sure you have the following requirements in place before beginning this post if you want to follow along.

  1. At least one machine that runs Windows. If you have more than one, learning how to handle numerous computers at once can be beneficial.
  2. PowerShell 7 – Despite the fact that the majority of the ideas are the same as in Windows PowerShell, you’ll use the most current version of PowerShell.
  3. Any remote machine you’d wish to query should have PowerShell Remoting enabled.

Using Get-Service in PowerShell to List Services

Enumerating what services are available on a local machine is one of the most fundamental activities you can do using PowerShell and Windows services. For instance, launch PowerShell and execute Get-Service to see what happens.

As seen in the picture below, executing Get-Service by itself will display all services on the local machine, along with their Status, Name, and DisplayName.

To locate Windows services, use Get-Service.To locate Windows services, use Get-Service.

PowerShell, like many other cmdlets, does not return all of the properties for each service. If you want to examine a service’s necessary services or its description, for example, pipe the output to Select-Object and use * to represent all attributes, as seen in the accompanying picture.

For service objects, inspecting all properties is a must.For service objects, inspecting all properties is a must.

Locating Services from a Distance

Maybe you’re on a network and want to see what services are available on one or more distant Windows machines. This might have been done in the old days of Windows PowerShell using the ComputerName argument, however that option no longer exists.

With PowerShell Core though, Locating Services from a Distance is still possible using two different methods; PowerShell Remoting CIM/WMI.

PowerShell Remoting and Get-Service

PowerShell Remoting is one approach to remotely check Windows services (PS Remoting). PS Remoting allows you to encapsulate any local command and run it in a remote session exactly as you would locally.

If you have PowerShell Remoting enabled on a remote computer, you may use Invoke-Command to perform Get-Service on that machine, as shown below.

In an Active Directory (AD) environment, the Credential parameter is not required.

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

Invoke-Command delivers all of the information supplied by Get-Service to Invoke-Command, and services are delivered to you as intended.

Command to InvokeCommand to Invoke

Take note of the additional PSComputerName property. This is the property that Invoke-Command returns. You may also use a simple script to enumerate services on a large number of distant systems.

Get-Credential $cred = Get-Credential $cred = Get-Credential $cred foreach ($name in $computers) $computers = Get-Content -Path ‘C:computers.txt’ $computers = Get-Content -Path ‘C:computers.txt’ $computers = Get-Content -Path ‘C: $services = Invoke-Command -ComputerName; $services = Invoke-Command -ComputerName; $services = Invoke-Comm -Credential $name -ScriptBlock $cred [pscustomobject] Get-Service @Services = $services @ComputerName = $name @ComputerName = $name @ComputerName = $name @ComputerName = $name

CIM/locating WMI’s services

PowerShell with Get-Service may not be appropriate in certain instances. Instead, you may use a Session on CIM to query CIM/WMI. You don’t need to utilize PowerShell Remoting if you’re utilizing a Session on CIM.

You may use CIM to locate and manage services by:

  1. Make a PSCredential object out of it. Because the two PCs in the following example are not in an Active Directory environment, we must utilize the Credential parameter.
  2. Create a Session on CIM using the computer’s name and the credential to use for authentication.
  3. To query the Win32 Service class, use Get-CimInstance.

$cred = Get-Credential $cimSession = New-CimSession -ComputerName $serverName -Credential $cred Get-CimInstance -CimSession $cimSession -ClassName Win32 Service

Much of the same information is provided, but it is structured differently, as you can see below.

Session on CIMSession on CIM

Creating and Terminating Services

PowerShell may also be used to launch and terminate services. There are a few options for doing this.

Start-Service and Stop-Service in PowerShell

The Start-Service and Stop-Service cmdlets are the first options. These cmdlets perform just as expected. You may use the pipeline or the Name parameter to utilize them, as demonstrated below.

## Use the Name argument to stop a service. ‘wuauserv’ is the name of the service. Stop-Service -Name -Name -Name -Name -Name $serviceName ## Use the pipeline to stop a service. Get-Service Stop-Service $wuauserv

The Name and DisplayName arguments of all *-Service cmdlets enable you to tab-complete service name values. Simply enter -Name, a space, and then start tapping the Tab key. It cycles through all of the services on the local computer, as you can see.

Starting as a service follows the same logic.

## Use the Name argument to stop a service. ‘wuauserv’ is the name of the service. Start-Service -Name is a command that starts a service. $serviceName ## Use the pipeline to stop a service. Get-Service Start-Service $wuauserv

Both the Stop-Service and Start-Service cmdlets are idempotent, which means that if a service is already stopped or started and you try to stop or start it while it is in that state, the cmdlets will simply skip over it.

You’ll need to wrap these commands in a scriptblock and use PowerShell Remoting to launch them remotely to start and stop remote services using PowerShell, as illustrated below.

Get-Credential $cred = Get-Credential $cred = Get-Credential $cred $serviceName = ‘wuauserv’ Start-Service -Name $using:serviceName -ComputerName SRV1 -ScriptBlock Invoke-Command -ComputerName SRV1 -ScriptBlock -Credential $cred

In this Invoke-Command article, you’ll learn about the $using construct and how to send local variables to remote scriptblocks.

Starting and Stopping Services with PowerShell and CIM

CIM may be used to start and terminate services, much as Get-Service. However, you can’t use Stop-Service and Start-Service cmdlets directly. Instead, you must call a method. Although less apparent, if you’re already using CIM to handle certain things, it can make sense to use it to manage service as well.

Use Get-CimInstance once more if you’re dealing with local services. This time, though, you must use the Filter option to narrow down the services to just the ones you want to stop or activate. Filtering the results (together with the Query parameter) is a wonderful method to narrow them down.

The following is an example:

  • Querying the Win32 Service class in the local computer’s CIM store for any services with the Type of Business set to automatic (StartMode=’Auto’).
  • Querying the Win32 Service class in the local computer’s CIM store for any services that are also stopped (State=’Stopped’).
  • Invoke-CimMethod receives all of those objects and runs the StartService method on each of them.

-Filter Get-CimInstance -ClassName Win32 Service “Invoke-CimMethod -MethodName StartService” | “StartMode=’Auto’ And State=’Stopped’”

Using the StopService function and maybe modifying the State in the query to Started, the same code can also halt services.

The WQL language is supported through the Filter option. The about WQL help topic in PowerShell may teach you more about WQL.

Using PowerShell to start and stop remote services

So now that you know how to start and stop services locally, you can use the same code to enumerate services on distant systems.

You can always wrap any code in a scriptblock and run it remotely using PowerShell Remoting. This course will presume that you are aware of this possibility from here on out and will not cover it in all instances.

To start and stop services remotely, you can use a Session on CIM again. You could either reuse the Session on CIM you created above or if you removed it, create another one as shown below.

$cred = Get-Credential $serverName = ‘SRV1’ $cimSession = New-CimSession -ComputerName; $cimSession = New-CimSession -ComputerName; $cimSe -Credential $serverName $cred

Once you have created a Session on CIM, use the Invoke-CimMethod cmdlet and don’t forget to remove that Session on CIM when you’re done.

-CimSession Get-CimInstance -Filter $cimSession -ClassName Win32 Service “StartMode=’Auto’ And State=’Stopped’” | Remove-CimSession -CimSession Invoke-CimMethod -MethodName StartService $cimSession

You don’t have to use a Session on CIM to manage remote Windows services. If both the local and remote computer are a member of an AD domain, you could use Get-CimInstance and the ComputerName parameter. However, if you must pass a credential to the remote computer, you must use a Session on CIM.

A Session on CIM also is a bit more efficient if you need to execute multiple CIM methods or perform CIM queries on the remote computer because it re-uses the same session instead of having to create a new one for each task.

Restarting a Service using PowerShell

Perhaps you wish to restart an already running service. With PowerShell, this isn’t an issue. You have two options once again.

Using the Start and Stop-Service Commands

If you want to restart a running service, you may use the Stop-Service and Start-Service cmdlets to do it in a number different ways, as illustrated below.

## Use the Name argument to restart a service. Stop-Service -Name $serviceName = ‘wuauserv’ $serviceName $serviceName $serviceName $serviceName $serviceName $serviceName $serviceName $serviceName $ $serviceName ## Use the pipeline and PassThru parameters to restart a service. Stop-Service -Name $serviceName = ‘wuauserv’ Start-Service $serviceName -Passthru

You may use the PassThru option to send the service object that Stop-Service or Start-Service just ran on and execute some other sort of operation on that service through the pipeline. The PassThru option simply instructs the cmdlet to return the object to the pipeline, allowing it to be piped to other cmdlets.

Using the PowerShell cmdlet Restart-Service

You should use the Restart-Service cmdlet to keep the code for restarting a service in PowerShell to a minimum. This cmdlet performs precisely what you’d expect it to do and works in the same way as the other service cmdlets.

For example, instead of piping the output of Get-Service directly to Restart-Service as shown above, you might pipe the output of Get-Service directly to Restart-Service as shown below.

## Use the Name argument to restart a service. ‘wuauserv’ is the name of the service. Get-Service -Name is a command that returns the name of a service. Restart-Service | $serviceName

Changing the Type of Startup

Another popular task when managing services is Changing the Type of Startup. The Type of Business is the attribute that dictates what the services do when Windows boots up. You have a few options.

  • Autonomous (The service automatically starts when Windows does)
  • unable to work (The service will never start)
  • a guide (The service is available to start but must be done manually)
  • Delay – Automate (The service starts automatically but is delayed once Windows boots)

Let’s imagine you just need to know the starting type of a service. This may be found via Get-Service or CIM.

If you’re looking for the Type of Business using Get-Service, you’ll see that it’s referred to as Status and is represented by the Status property.

(Get-Service wuauserv -Name) Type of startup: automatic

You may immediately examine all of the startuptype values for all of the services by using Group-Object, as shown below. This snapshot displays all of the starting types that a service may have (in the Name column).

Command to Group ObjectsCommand to Group Objects

Set-Service may be used to alter the current Type of Business once you know what it is.

The starting type is set to Disabled in the example below.

Set-Service -Name <some service name> -StartupType Disabled

The StartupType argument, like the Name and DisplayName parameters, enables you to tab-complete all of the different Type of Businesss to create a service.

Making Use of the Registry

You may also use PowerShell to change the service Type of Business in the registry. The HKLMSystemCurrentControlSetServices registry entry stores all Windows services. The Type of Business is represented by a REG DWORD value named Start in each service child key (excluding Start time has been pushed back.).

To set the Type of Business for a service in the registry via PowerShell, use the Set-ItemProperty cmdlet. The below snippet is Changing the Type of Startup of the wuauserv service to automatic.

Set-ItemProperty $serviceName = ‘wuauserv’” HKLM:SystemCurrentControlSetServices $serviceName” -Name “Beginnnnnnnnnnnnnnnnn “-Type DWORD -Value 2

To map each REG DWORD value to the Type of Business you want, you’ll need a map. A useful table may be seen below.

Value of REG DWORD Type of Business
0 The boot loader has loaded (but not started) the system. Then it began during kernel startup.
1 After services with a start parameter of 0 are started during kernel startup, this service is started.
2 Automatically. smss.exe (session manager) or services.exe start it (services controller).
3 Manually. The Service Control Manager initiated the project (SCM).
4 Disabled
5 Start time has been pushed back.

REG DWORD REG DWORD REG DWORD REG DW

You may also set the Type of Business to Start time has been pushed back. by setting the DelayedAutoStart registry value to 1 via Set-ItemProperty -Path “HKLM:SystemCurrentControlSetServices<service name>” -Name “DelayedAutostart” -Value 1 -Type DWORD.

Steps to Follow

By now you should know the basics of restarting service with PowerShell along with managing them. If you’d like to learn more about managing services with PowerShell, be sure to check out PowerShell’s help content with Get-Help <cmdlet name>.

There are a lot of parameters not addressed in this article, so don’t worry if your specific use case isn’t covered. Always check the help text to make sure a parameter isn’t already set.

Additional Reading

At least two external links to other sites where the reader may discover further information should be provided to the reader.

The “powershell get-service path” is a command-line tool that allows users to manage Windows Services. It can be used in PowerShell to view and stop services, as well as start, stop, pause, resume, or check the status of services.

Frequently Asked Questions

How do I view Windows services in PowerShell?

A: You can use the Get-Service cmdlet, which will give you a list of services that are running on your operating system.

How do I get a list of services in PowerShell?

A: You can use the Where-Object cmdlet to get a list of all services on your system.

How do I run a service in PowerShell?

A: You can run a service in PowerShell by executing this command.
C:\Program Files (x86)\Windows Kits\10\bin>sc start .cmd

Related Tags

  • powershell script to start and stop windows services
  • powershell start-service
  • restart-service powershell
  • powershell get-service remote computer
  • get-service status powershell