How to Manage Windows Services with PowerShell?

You can handle Windows providers not solely from the providers.msc snap-in or sc.exe command line software, but in addition utilizing PowerShell. In this text we’ll contemplate completely different eventualities of managing Windows providers with PowerShell.

PowerShell Cmdlets Used to Manage Windows Services

There are eight primary Service cmdlets to view the state of Windows providers and handle them. To get the complete checklist of service administration cmdlets, run this command:

Get-Help *-Service

basic powershell cmdlets to manage windows services

  • Get-Service — permits to get the providers on an area or distant pc each in operating or stopped state;
  • New-Service – creates a service. The cmdlet creates a brand new entry for a Windows service within the registry and within the service database;
  • Restart-Service – restarts a service. The cmdlet sends the restart message by means of the Windows Service Controller;
  • Resume-Service – resumes a service. The cmdlet sends a resume message to Windows Service Manager;
  • Set-Service — modifications the settings of an area or distant service, together with its state, description, displayed title or startup mode. You may use this cmdlet to begin, cease or droop a service;
  • Start-Service – begins a service;
  • Stop-Service – stops a service (the cmdlet sends a stopping message to Windows Service Manager);
  • Suspend-Service – suspends a service. A suspended service continues to be operating, however it doesn’t do something until it’s resumed utilizing( for instance, with the Resume-Service cmdlet).

You can get an in depth description and examples of utilizing a specific cmdlet with Get-Help:

Get-Help Start-Service

Get-Help

How to Check Windows Service Status with Get-Service?

You can get the checklist of providers and their state (Running/Stopped) on an area or distant pc utilizing the Get-Service cmdlet. The –Name parameter permits to choose providers by title. The service title may be specified utilizing the wildcard character *.

Check Windows Service Status with Get-Service

If you have no idea the precise service title, you could find it by its displayed title utilizing the –ShowName parameter. You can use the checklist of values and wildcards.

powershell get services

Use the Get-Service cmdlet with the -PcName parameter to get the service standing on a distant pc. You can question the service standing on a number of distant computer systems without delay by specifying their names separated by commas. For instance, the command proven under will get the Spooler service standing on the distant computer systems ny-prnt1 and ny-prnt2.

Get-Service spooler –PcName ny-prnt1,ny-prnt2

Status Name ShowName
------ ---- -----------
Running spooler Print Spooler
Stopped spooler Print Spooler

To show all of the properties of a service, use the Select-Object cmdlet:

Get-Service spooler | Select-Object *

Get-Service properties

The Select-Object cmdlet permits to get particular properties of a service. For instance, you need to view the title, standing and out there choices of the Spooler service:

Get-Service Spooler | Select ShowName,Status,ServiceName,Can*

Get-Service state

The Get-Service cmdlet has two parameters that enable you to view the service dependencies:

  • -DependentServices permits to show the providers depending on the given service
  • -RequiredServices shows the providers the given service is dependent upon

The following command shows the providers required to begin the Spooler service:

Get-Service –Name Spooler -RequiredServices

Get-Service RequiredServices

The following command reveals the providers that rely on Spooler:

Get-Service –Name Spooler -DependentServices

Get-Service DependentServices

If you need to discover the providers with the particular state or parameters, use the Where-Object cmdlet. For instance, let’s get the checklist of operating providers:

Get-Service | Where-Object

Get-Service: list of running services

To show the providers with the guide startup kind, run this command:

Get-Service | Where-Object

get service with the manual startup type

Use the next PowerShell script to verify if a selected Windows service exists on a present pc:

if (Get-Service "ServiceCheck" -ErrorAction SilentlyContinue)

How to Stop, Start, or Restart Service with PowerShell?

You can cease a service utilizing the Stop-Service cmdlet. To cease the Spooler, run the command:

Stop-Service -Name spooler

The Stop-Service cmdlet doesn’t show something after execution. To see the consequence, use the -PassThru parameter.

Stop-Service PassThru

Please word that not each service may be stopped. If there are any dependent providers, you will notice an error:

Cannot cease service as a result of it has dependent providers. It can solely be stopped if drive flag set.

Cannot stop service because it has dependent services. It can only be stopped if force flag set.

To drive a service to cease, use the –Force parameter. You ought to do not forget that all dependent providers will even cease:

Stop-Service samss –Force -Passthru

The following command will cease the desired providers (bits, spooler) if they’re in “Running” state:

get-service bits,spooler | the place | stop-service –passthru

stop services in the running state

Sometimes providers grasp up within the “Stopping” state and have to be .

The Start-Service cmdlet begins a stopped service:

Start-Service -Name spooler -PassThru

Start-Service

A service received’t begin, if any of its dependent providers is stopped. To discover and begin them use the next PowerShell one-liner:

get-service samss | Foreach

powershell start a service with all dependences

The Suspend-Servce cmdlet can pause providers in the event that they assist this state. To study if a service may be suspended, use the Get-Service cmdlet with the CanPauseAndContinue property.

Get-Service samss | Format-List title, canpauseandcontinue

Get-Service canpauseandcontinue

To show the checklist of all providers that could be paused, run this command:

Get-Service | Where-Object 

get service with the canpauseandcontinue option

Let’s droop SQLBrowser service:

Suspend-Service -Name SQLBrowser

Suspend-Service

To resume a suspended service, use the Resume-Service cmdlet:

Resume-Service -Name SQLBrowser

Resume-Service

The following command will resume all suspended providers:

get-service | where-object | resume-service

The Restart-Service cmdlet will restart a service:

Restart-Service -Name spooler

Restart-Service

This command begins all stopped community providers on a pc:

get-service internet* | where-object | restart-service

These instructions don’t have the –PcName parameter, however you’ll be able to run them on a distant pc utilizing the Invoke-Command cmdlet or a pipe.

For instance, to restart a print spooler on the distant pc ny-prnt1, run the command:
Get-Service Spooler -PcName ny-prnt1 | Start-Service

Using Set-Service to Change Service Settings

The Set-Service cmdlet permits you to change any parameters or settings of a service on an area or distant pc. Since the state of a service is a property, you should use this cmdlet to begin, cease or droop a service. Set-Service has the -StartupType parameter that enables to change the startup kind of a service.

Let’s change the Spooler startup kind to computerized:

Set-Service spooler –startuptype computerized –passthru

change the startup type of the Print Spooler to Automatic

You can set the guide startup kind:

Set-Service spooler –startuptype guide –passthru

set the manual startup type Set-Service

How to Create or Delete a Windows Service through PowerShell?

New-Service – is a cmdlet to create a brand new service in Windows. Specify the title and the executable file for the brand new service (you’ll be able to even ).

Let’s create a brand new service with the title TestSvc:

new-service -name TestSvc -binaryPathName "C:WINDOWSSystem32svchost.exe -k netsvcs"

new-service create service with powershell

Get the details about the startup kind and outline of the service utilizing the Get-WmiObject cmdlet.

get-wmiobject win32_service -filter "title='testservice'"

get-wmiobject win32_service

You can change the settings of the brand new service utilizing the next command:

Set-Service -Name TestSvc -Description ‘My Service’ -StartupType Manual

change service startup type to manual

To delete a service, run this command:

(Get-WmiObject win32_service -Filter ″title=′TestSvc′″).delete()

Change the User Account that Runs the Windows Service

You can use PowerShell so as to change consumer account used to begin a service. Get the title of the account used to begin TestSvc:

get-wmiobject win32_service -filter "title='TestSvc'" | Select title,startname

get the user account to run the service

To change the username and password for a Windows Service, run the next instructions:

$svc = get-wmiobject win32_service -filter "title='TestSvc'"
$svc.GetMethodParameters("change")

The checklist of the Change() technique parameters is displayed. Count the place the StartName and StartPassword parameters are: they positioned on the 20th and 21st locations respectively.

change runas user account of a windows service

$svc | Invoke-WmiMethod -Name Change –ArgumentList @ ($null,$null,$null,$null,$null,$null,$null, $null,$null,$null,$null,$null,$null,$null,$null,$null, $null,$null,$null,"Administrator","!123Pa$$w0rd")

Or you’ll be able to enter the title of a account (the account password isn’t specified on this case).

As you’ll be able to see, PowerShell makes it simple to handle Windows providers. You can create, cease, begin or resume providers, and alter their properties. Most cmdlets enable to handle providers on distant computer systems.

Check Also

KVM: How to Expand or Shrink a Virtual Machine Disk Size?

In this text we’ll speak about how to resize KVM digital machine disks for standard …

Leave a Reply

Your email address will not be published. Required fields are marked *