Powershell Filter Eventlog Message String after Account Name

Dear Community

I am trying to filter a string from an eventlog message with powershell and write it into a log file.  
Actually I use the following command:
get-winevent -FilterHashtable @{Logname='Security';ID=4740}  -MaxEvents 1 | FL

I attached the output for the command above (Filter-accound-name.png). In that output I tagged a string with yellow. Is is possible to extract that only that string, which is the account name, and set it into a variable?

There are commands like select-string or findstr, but I was not able to filter only the account name with these commands.

Thank you in advance for your help

Best regards
steffeninf
filter-account-name.PNG
SilvanSystem EngineerAsked:
Who is Participating?
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.

oBdACommented:
The short version (relies on the target user name being the first property of the event data):
$TargetUserName = (Get-WinEvent -FilterHashtable @{Logname='Security'; ID=4740} -MaxEvents 1).Properties[0].Value

Open in new window

Or (more properly solved by using the event xml) if you want to retrieve all of the properties and convert them to a custom object, you can use something like this:
$Event = Get-WinEvent -FilterHashtable @{Logname='Security'; ID=4740} -MaxEvents 1 |
	% {
		$Result = $_ | Select-Object -Property TimeCreated, ProviderName, Id
		([xml]$_.ToXml().Replace("xmlns=", "dummy=")).SelectNodes("/Event/EventData/Data") |
			% {
				Add-Member -InputObject $Result -MemberType "NoteProperty" -Name $_.Name -Value $_.InnerText
			}
		$Result
	}
$Event
"TargetUserName: " + $Event.TargetUserName

Open in new window

Working with the .Message property isn't really ideal, see 'Get-WinEvent does not return the content of the Event Message in V3 CTP2', https://connect.microsoft.com/PowerShell/feedback/details/716533/get-winevent-does-not-return-the-content-of-the-event-message-in-v3-ctp2
2

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
SilvanSystem EngineerAuthor Commented:
Great it works as expected. But I still have a question, how did you know that the user name is stored in "Properties[0]"?
0
oBdACommented:
Using the second-most important PS command after Get-Help (namely Get-Member), and some educated guessing.
For speed reasons, assign the result of your query to a variable:
$e = Get-WinEvent -FilterHashtable @{Logname='Security'; ID=4740} -MaxEvents 1

Open in new window

Then examine that object (gm is an alias for Get-Member):
$e | gm

Open in new window

You'll get a bunch of methods and properties, and the "Properties" property looks interesting. You check it out
$e.Properties

Open in new window

and you find the properties as listed in the "Friendly View" of the event viewer's details.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.