How to list the results of a search with regex in a directory?


I have a directory with some C# files. In these files there are several SQL query codes.

I need to list all SELECT commands in these files using PowerShell. Commands start with SELECT and end with ; and can have more than one line, like the example:

SQL = "SELECT t.a, t.b, t.c FROM uf_GetResultSet(4, 1, 0, 0, 'G', 0, 0, 0) t";
SQL = "SELECT t.a, t.b, t.c" + 
      "FROM uf_GetResultSet(4, 3, 0, 0, 'C', " + idSomeThing.ToString() + ", 0, 0) t";

The regex pattern SELECT .+[\r\n]*.+"; it works perfectly for me using Notepad++, but I don't know how to adapt it in PS.


I came to the following command:

PS> select-string -path *.cs -pattern "(?smi)(?<sql>SELECT .+?);" | foreach {$_.matches} | foreach {$_.groups['sql']} | select value

But it returns SELECTs for only one row.

Scroll to Top