# Log filtering to txt

**URL:** <https://forums.powershell.org/t/log-filtering-to-txt/16031>\
**Category:** PowerShell Help\
**Created:** [March 20, 2021, 3:24pm UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031 "2021-03-20T15:24:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![crysti](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@crysti](https://forums.powershell.org/u/crysti)\
**Post date:** [March 20, 2021, 3:24pm UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/1 "2021-03-20T15:24:27Z")

</div>

Hello,  
I’m novice to powershell. I want a script to write to a txt file when log 4720 is generated the following information: date and time of event ID, Subject: Account Name, New Account: Account Name.  
I succeeded to extract only the date and time.

_Get-EventLog -Log Security -Newest 30 | Where-Object { $\_.EventID -eq 4720 } | Select-Object -Property TimeGenerated_

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 21, 2021, 12:59am UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/2 "2021-03-21T00:59:47Z")

</div>

crysti,

welcome to the forums

First of all - when you post code, error messages, sample data or console output format it as code, please.

Here you can read how that works: Guide to Posting Code.

> [@Guide to Posting Code - Redux](https://forums.powershell.org/t/guide-to-posting-code-redux/15797):
>
> Discourse uses [Markdown](https://www.markdownguide.org/getting-started/) for text formatting. There is a reference sheet and quick tutorial here: [Markdown Reference](https://commonmark.org/help/) You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem. For larger code blocks, place a line containing three backticks above and below your code. For example ``` # 256-Color Foreground & Background Charts echo “`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m” foreach ($fgbg in 38,48) { #…

Now to your challenge … unfortunately the information you’re after are _hidden_ in the “Message” property of the returned object. So you have to do some regex acrobatics to cut them out of the rest of the message body.

Try the following snippet:

```auto
$FilterHashTable = @{
    LogName = 'Security'
    ID = 4720
}
Get-WinEvent -FilterHashtable $FilterHashTable -ComputerName 'TargetedDC' -MaxEvents 30 |
ForEach-Object {
    $_.Message -match '(?smi)Account Name:\s*(\S*)\s*[\d\D]*Account Name:\s*(\S*)\s*'
    [PSCustomObject]@{
        TimeCreated = $_.TimeCreated
        SubjectAccountName = $Matches[1]
        NewAccountName = $Matches[2]
    }
}

```

Of course you should replace the value ‘TargetedDC’ with the name of the DC you want to query. 😉

---

<div class="post-metadata">

**Author:** ![crysti](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@crysti](https://forums.powershell.org/u/crysti)\
**Post date:** [March 21, 2021, 10:46am UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/3 "2021-03-21T10:46:19Z")

</div>

> [@Olaf](#):
>
> ```auto
> $FilterHashTable = @{
> LogName = 'Security'
> ID = 4720
> }
> Get-WinEvent -FilterHashtable $FilterHashTable -ComputerName 'TargetedDC' -MaxEvents 30 |
> ForEach-Object {
> $_.Message -match '(?smi)Account Name:\s*(\S*)\s*[\d\D]*Account Name:\s*(\S*)\s*'
> [PSCustomObject]@{
> TimeCreated = $_.TimeCreated
> SubjectAccountName = $Matches[1]
> NewAccountName = $Matches[2]
> }
> }
> 
> ```

Thank you very much, it worked for the first time!

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [March 21, 2021, 12:47pm UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/4 "2021-03-21T12:47:10Z")

</div>

Another way to do this would be to parse the XML:

```auto
$filterHashtable = @{

    LogName = 'Security'
    ID = 4720

}

$events = Get-WinEvent -FilterHashtable $filterHashtable

foreach ($event in $events) {

    $h = @{}
    [xml]$xEvent = $event.toXML()
    $xEvent.Event.EventData.Data | ForEach-Object {

        $h.add($_.Name,$_.'#text')

        [PSCustomObject] @{

            TimeCreated = $xEvent.Event.System.TimeCreated.SystemTime
            SubjectAccountName = $h.SubjectUserName
            NewAccountName = $h.TargetUserName

        } #end PSObject

    } #end Foreach-Object

} #end foreach $event

```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [March 24, 2021, 4:29pm UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/5 "2021-03-24T16:29:00Z")

</div>

Another advantage is a serious performance gain when using FilterHashTable. FilterXPath also does the same.

$XPath = “Event[System[EventID=4720]”

```auto
$events = Get-WinEvent -FilterXPath $XPath

```

In your $XPath, you can have conditionals and narrow down even further for even better performance gains.

---

<div class="post-metadata">

**Author:** ![crysti](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@crysti](https://forums.powershell.org/u/crysti)\
**Post date:** [March 27, 2021, 5:47pm UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/6 "2021-03-27T17:47:33Z")

</div>

Thank you for the information

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/log-filtering-to-txt/16031/7 "2024-05-16T20:26:59Z")

</div>

Closed on May 16, 2024, 8:26pm UTC


