19

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
  • Your post is a little vague with the 'first line' language. Are you looking to grab the data without the headers? Is that what you mean by first line? Commented Mar 8, 2016 at 16:00
  • I mean literally the first line. I'll try to rephrase. Commented Mar 8, 2016 at 16:19

2 Answers 2

29

Splitting the string on newline into an array and then taking the first of the array will work, although it might not be the most efficient.

($eventResult.Message -split '\n')[0]
Sign up to request clarification or add additional context in comments.

2 Comments

Not efficient but it worked :) Kudos to you my friend.
(-split $eventResult.Message)[0]
17

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

2 Comments

Using [Environment]::NewLine is key to this code working cross platform.
$eventResult.Split([Environment]::NewLine) | Select -Skip 1 to skip first line but obtain rest of it. More details can be found here learn.microsoft.com/en-us/powershell/module/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.