PowerShell Select-String Output to a Variable

I have found this link, which has helped me form the majority of my code. However, in these examples, the output is being piped to a file. I could turn around and read the contents of the file, but I would rather get the results into a variable (btw, there will only be one line that matches, if any). If I use:

$s = Select-String -Path $ERROR_LOG -Pattern $regex

then $s contains a string in the format of:

filename:line#:filedate - full contents of entire line

e.g.:
error.log:49:2015/07/07 12:15:30 - blah blah blah blah: Incorrect syntax near '.84603e-005'.

However, I want only the data that matched the regex ("Incorrect syntax near '.84603e-005'.") to be returned.

Any easy way to accomplish that?
LVL 15
Doug BishopDatabase DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
What you think is the content of the var is not correct. You see a string representation from PowerShell trying its best to provide useful information (by calling the ToString() method). If you want to see the real content, always use something like
$s | format-list *

Open in new window

or (to only see properties, but no values)
$s | get-member

Open in new window

The complete line is contained in Line, so you probably want to use
$s = Select-String -Path $ERROR_LOG -Pattern $regex | select -Expand Line

Open in new window

That last part extracts only a single property, and unwraps it. For string properties you just get the string.
Or more clearly:
$s = Select-String -Path $ERROR_LOG -Pattern $regex | select Line

Open in new window

creates an object having a single (user) property called "Line", and you need to use $s.Line to access it.
With -Expand as shown above, $s contains the line directly.
Doug BishopDatabase DeveloperAuthor Commented:
Both methods (using -Expand Line and $s.Line) only remove the filename and line number. I still get the full line (btw, the reference originally to the filedate was incorrect; the line starts with a timestamp, so "2015/07/07 12:15:30 - " is part of the row containing the regex expression).

Instead of "2015/07/07 12:15:30 - blah blah blah blah: Incorrect syntax near '.84603e-005'." I want to have only "Incorrect syntax near '.84603e-005'." which is the portion of the line that matched the regex expression.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry, didn't read the question correctly.
The Matches property is what you need, but it contains more than one match if your regex is not unique. With a simple regex not containing groups (patterns in parens):
$s = (Select-String -Path $ERROR_LOG -Pattern $regex).Matches[0].Value

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Doug BishopDatabase DeveloperAuthor Commented:
Exactly what I wanted! Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.