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:Usersx> get-service | Select-String "nginx"
PS C:Usersx> get-service | Select-String -Pattern "nginx"
PS C:Usersx> get-service | Select-String -Pattern 'nginx'
I get no output.
So how do I "grep" in Powershell?
2
Answers
This should do it
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:
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: