Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter the output of a command as if it was text

Tags:

powershell

I have a simple question, but I am also a beginner in PowerShell. I think it has to do with the fact that the output of the Get-Process command (alias ps) is objects and not text.

I want to get a list of the services running that have the name "sql" in them.

This is what I tried so far, but every attempt returns nothing:

Get-Service | where {$_ -match 'sql'}

Get-Service | where {$_ -like 'sql'}

Get-Service | Select-String sql

I am looking for a pattern that lets me treat the output of every command as searchable text.

like image 676
buckley Avatar asked Jul 09 '11 13:07

buckley


People also ask

How do you filter output command?

You can use the | { begin | exclude | include } regular-expression option to filter the display command output.

Which two commands are used to filter only selected properties?

Using the Where-Object and Select-Object commands allows you to easily control which items you are working on in PowerShell. You can use these commands to either filter the data you are viewing or to limit actions (such as stopping services or removing files) to those that match the filters you define.

What two Linux commands can be used for pattern matching and filtering of output?

Grep, Egrep, Fgrep, Rgrep Commands These filters output lines matching a given pattern.

What does filter do in PowerShell?

Introduction to Filtering in PowerShell. Filtering refers to the process of restricting the output of a cmdlet or a statement based on certain conditions.


2 Answers

Just forget it :o)

Outputs are objects. You are right, and you are going to use this.

So mjolinor has the shortest answer, but for your knowledge just test:

Get-Service | Get-Member

So you will understand that

Get-Service | Where-Object {$_.name -match ".*sql.*" }

also works, and there you've got your text as a property of the object.

like image 140
JPBlanc Avatar answered Oct 18 '22 22:10

JPBlanc


Most answers here focus on finding the service name with "sql" in the name, not on filtering the entire output as if it was text. Also, the accepted answer uses a non-PowerShell function, "findstr".

So, granted, what follows is not the most elegant solution, but for sake of completeness I would like to provide the 100% PowerShell solution that takes the question of the OP literally:

(get-Service | Out-String) -split "`r`n" | Select-String sql
  • We need Out-String, because using the solutions provided in other answers doesn't provide us the full text output of the Get-Service command, only the Name parameter.
  • We need to split on newlines, because Select-String seems to treat the entire text as one long string, and returns it as a whole, if "sql" is found in it.
  • I use Select-String instead of findstr, because findstr is not a PowerShell function.

This is a purist answer, and in practice, for this specific use-case, I would not recommend it. But for people coming here through Google Search based on the question title, this is a more accurate answer...

like image 32
Wouter Avatar answered Oct 19 '22 00:10

Wouter