CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Managing Services

Service is a program that runs in the background; and we can start, stop or pause/resume the service depending on the need or requirement. PowerShell provides commands, to manage the services.  I am going to explain, how to manage the services through this Article “PowerShell – Managing Services”.

Display List of Services: Get-Service cmdlet

PowerShell provides Get-Service cmdlet, to display the list of Services available in the Local or Remote System. Just you type “Get-Service” at the PowerShell command prompt; will display the list of all the services currently available in the Local System.

PS C:\> Get-Service

Status   Name               DisplayName
------   ----               -----------
Running  AdobeARMservice    Adobe Acrobat Update Service
Running  AeLookupSvc        Application Experience
Running  AGSService         Adobe Genuine Software Integrity Se...
...........
...........
Running  WwanSvc            WWAN AutoConfig

How to display the list of Services currently Running?

Get-Service returns all the services which are currently available in the system. To display the services which are currently RUNNING, we must take the help of the “Where-Object” command.  This will apply the given condition on the result; and will display the result if the condition is satisfied. I will explain more about the “Where-Object” cmdlet, in another Article.

The below command is useful to list the services which are currently running on the local system:

PS C:\> Get-Service | Where-Object Status -EQ "Running"

Status   Name               DisplayName
------   ----               -----------
Running  AdobeARMservice    Adobe Acrobat Update Service
Running  AGSService         Adobe Genuine Software Integrity Se...
.......
.......
Running  WwanSvc            WWAN AutoConfig

Stop the Service: Stop-Service cmdlet

To STOP the service, PowerShell provides a Stop-Service cmdlet. To STOP the Service, we need to pass the name of the service through its’ “-Name” parameter.

The above list shows the list of currently available services in the Local System. Lets’ try to STOP the service “AdobeARMservice”:

PS C:\> Stop-Service -Name AdobeARMservice
PS C:\>

The above command stops the service “AdobeARMservice”. Do you want to check whether the service is STOPPED.?

PS C:\> Get-Service | Where-Object Name -EQ "AdobeARMservice"

Status   Name               DisplayName
------   ----               -----------
Stopped  AdobeARMservice    Adobe Acrobat Update Service

Observe that, “AdobeARMservice” was STOPPED.

Start the Service : Start-Service cmdlet

Start-Service command is used to start the service which is available from the Local system or Remote system. Lets’ start the service, what we have stopped from the above Stop-Service command. The name of the service we want to start should pass through the “-Name” parameter.

PS C:\> Start-Service -Name AdobeARMservice
PS C:\>

Suspend the Service: Suspend-Service cmdlet

Suspend-Service cmdlet is used to suspend (or pause) the service which is running on the Local or Remote system. That means the actions of the service will be temporarily PAUSED until the service is resumed.

Lets’ try to suspend the above service:

PS C:\> Suspend-Service -Name AdobeARMservice
Suspend-Service : Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be suspended because the service does not support being suspended or resumed.
At line:1 char:1
+ Suspend-Service -Name AdobeARMservice
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Suspend-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotSuspendServiceNotSupported,Microsoft.PowerShell.Commands.SuspendServiceCommand

Suspend-Service : Service 'Adobe Acrobat Update Service (AdobeARMservice)' cannot be suspended due to the following error: Cannot pause AdobeARMservice service on computer '.'.
At line:1 char:1
+ Suspend-Service -Name AdobeARMservice
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Suspend-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotSuspendService,Microsoft.PowerShell.Commands.SuspendServiceCommand

PS C:\>

Why we encountered this error.? As mentioned in the error itself; the service should support suspend or resume. How to verify whether the Service supports suspend or resume.? I will explain this in another article.

Lets’ try the Suspend-Service command on the service which supports suspend or resume.

PS C:\> Get-Service | Where-Object Name -EQ stisvc

Status   Name               DisplayName
------   ----               -----------
Paused   stisvc             Windows Image Acquisition (WIA)

How to resume this service back to running state.? Below section explains this.

Resume the Service: Resume-Service cmdlet

Resume-Service command is used to resume the service, which is currently paused to running state. We need to pass the name of the service through the “-Name” parameter.

Lets’ resume the service; what we have paused through the above command.

PS C:\> Resume-Service -Name stisvc
PS C:\> Get-Service | Where-Object Name -EQ stisvc

Status   Name               DisplayName
------   ----               -----------
Running  stisvc             Windows Image Acquisition (WIA)

That means, “stisvc” is supporting suspend or resume states. Keep that in mind that, when the service is suspended; its’ actions are PAUSED. Its’ actions will only resume once we resume the service.

How to create a new service? : New-Service cmdlet

PowerShell provides a New-Service command to allow the creation of a new service. As I mentioned above, service is a program that runs in the background. In order to create a service, we must specify the name of the service through the parameter “-Name” and the complete path to the program through “-BinaryPathName“.

Lets’ create a service, MyTestService which runs “armsvc.exe” in the background. Note that, this is the same program the service “AdobeARMservice” is referring.

PS C:\> New-Service -Name MyTestService -BinaryPathName "C:\Program Files (x86)\Common Files\Adobe\ARM\1.0\armsvc.exe"

Status   Name               DisplayName
------   ----               -----------
Stopped  MyTestService      MyTestService

PS C:\> Start-Service -Name MyTestService
PS C:\> Get-Service -Name MyTestService

Status   Name               DisplayName
------   ----               -----------
Running  MyTestService      MyTestService

From the above sequence of commands, I have created “MyTestService” using the New-Service command; and start run the service using the “Start-Service” command. Finally, I have verified whether the “MyTestService” service is running, using the Get-Service command.

These are the PowerShell cmdlets, useful to manage the services. We will go through more commands, through my upcoming Articles.

(Raju)

PowerShell – Managing Services

Leave a Reply

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

Scroll to top