Getting first line of string in a variable using Powershell

Viewed 53599

This line of code:

$eventResult = Get-EventLog -Source ".net runtime" -LogName Application -Newest 1 | select -expandproperty message

Outputs a very long string into $eventResult.

What I'd like to do is grab the very first line of it.

This outputs the ENTIRE content of $eventResult:

$eventResult | select-object -first 1

However, Outputting the data into a file and then parsing it works like a charm:

$eventResult | out-file c:\output.txt
cat c:\output.txt | select-object -first 1

What am I missing here?

UPDATE: if the output is as follows:

 Line1...
 Line2...
 Line3...

Then all I want is "Line1..."

UPDATE2:

I edited the $eventResult (forgot the | select message).

2 Answers

Select-Object is returning the entire content of $eventResult because the entire content of $eventResult is the first object.

For the first line to be returned by this query each line in $eventResult needs to become its own object, e.g:

$eventResult.Split([Environment]::NewLine) | Select -First 1
Related