Is there a way to include the instance of a service in the Get-Service and Get-WmiObject commands?

Home / powershell / Is there a way to include the instance of a service in the Get-Service and Get-WmiObject commands?

Question:
I am trying to write a script that stops a service if it exists. The service I want to stop has multiple instances and I was trying to get the service based on the given instance name (it shows up as ServiceName$InstanceName).

The weird thing is when I run the following command:Get-Service -Name "ServiceName*" | Format-List -Property Name
or
Get-WmiObject -Class Win32_Service -Filter "name like ‘ServiceName%’" | Format-List -Property Name
The results look like:Name : ServiceName$InstanceNameA

Name : ServiceName$InstanceNameB

Name : ServiceName$InstanceNameC

Name : ServiceName$InstanceNameD

Name : ServiceName$InstanceNameE

Name : ServiceName$InstanceNameF
Given the above results, I assumed that if I wanted to search for a service named ServiceName with an instance of InstanceNameC I could do something like
Get-Service -Name "ServiceName$InstanceNameC"
or
Get-WmiObject -Class Win32_Service -Filter "name=’ServiceName$InstanceNameC’"
but it’s not working as I would expect.

I guess the long winded question is: Can I find/get a specific service instance by passing it the proper service name of ServiceName$InstanceName?


Answer:
I’m clearly not experienced with powershell, but just found that adding the backtick (`) to escape the dollar sign that separates the service name from the instance name allows the Get-Service to work as I expected.

Here’s what worked:Get-Service -Name "ServiceName`$InstanceNameC"
Read more

Leave a Reply

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