1

When I issue get-service command, I get the entire list of services.

I want to list only a specific service.

The service in question is being listed in the output of get-service:

Running  nginx              nginx

But if I try to use any of the following:

PS C:\Users\x> get-service | Select-String "nginx"
PS C:\Users\x> get-service | Select-String -Pattern "nginx"
PS C:\Users\x> get-service | Select-String -Pattern 'nginx'

I get no output.

So how do I "grep" in Powershell?

2
  • 3
    try select-object, as get-service return isn't returning string but object, try get-service | select-object Status,Name | select-string nginx, I don't know how to prettify it, but I think you can try to add other or flatten the result. Commented Jun 30, 2021 at 7:13
  • why do you use Select-String when Get-Service already has many filtering options? Commented Jun 30, 2021 at 7:29

2 Answers 2

4

An essential thing to note is that PowerShell cmdlets always output objects, not simple strings - even when the output seems to be a string, it is really a System.String object. You can check the type of the output of a command (and the associated properties and methods) using Get-Member:

Get-Service | Get-Member

   TypeName: System.ServiceProcess.ServiceController

Name                      MemberType    Definition
----                      ----------    ----------
Name                      AliasProperty Name = ServiceName
RequiredServices          AliasProperty RequiredServices = ServicesDependedOn
Disposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)
Close                     Method        void Close()
Continue                  Method        void Continue()
...

As mentioned by others, Get-Service (and other cmdlets) has built-in filtering for what you're trying to do, but a more general alternative, which is more flexible (though often slower) is Where-Object:

Get-Service | Where-Object {$_.Name -like 's*' -and $_.Status -eq 'Running'}

Status   Name               DisplayName
------   ----               -----------
Running  SamSs              Security Accounts Manager
Running  SCardSvr           Smart Card
Running  Schedule           Task Scheduler
Running  SecurityHealthS... Windows Security Service
Running  SENS               System Event Notification Service
...
Sign up to request clarification or add additional context in comments.

Comments

2

This should do it

Get-Service -Name "nginx"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.