TExcelTAD
Home › Programming for Security: Python, Bash, PowerShell & C Tools › Parsing CSV and JSON Event Exports in PowerShell

Parsing CSV and JSON Event Exports in PowerShell

Analysts rarely read Windows event logs on the machine that wrote them. Events arrive as exports: a CSV from Get-WinEvent … | Export-Csv, JSON lines from a log shipper or a SIEM query, an EDR's process-tree download. The work is the same each time: load the records as objects, pull the interesting values out of nested fields, group and filter them, and write a short, reproducible answer. This lesson does that in PowerShell with a JSON export of process-creation events and a CSV of logons, all synthetic, from lab hosts under lab.test. The event IDs themselves are explained in The Windows Registry and Event Log, which already counted failed logons from a CSV; here the data is nested and the question is about processes.

Definitions

  • Event 4688 (Security log, "A new process has been created"): new process name, parent process name, command line (when the policy "Include command line in process creation events" is on), account.
  • Sysmon event 1 (Process Create): the same plus hashes, parent command line and a process GUID; preferred where Sysmon is installed.
  • Event 4624 (successful logon) with LogonType: 2 interactive, 3 network, 10 remote interactive (RDP).
  • ConvertFrom-Json: turns JSON text into PSCustomObjects; nested objects become nested properties ($e.Event.Data.CommandLine).
  • Calculated property: @{Name='X'; Expression={ … }} in Select-Object adds a column computed from each object.

Suspicious process trees from a JSON export

The export holds one JSON object per line, in a nested shape like many log shippers produce. The script flattens it, then applies three hunting rules: an Office program starting a shell, an encoded PowerShell command (decoded on the spot), and a process running from a user-writable folder.

powershell · try it yourself
$jsonl = @'
{"host":"ws1.lab.test","time":"2026-01-15T09:12:00Z","event":{"id":4688,"data":{"User":"LAB\\alice","Parent":"C:\\Windows\\explorer.exe","Image":"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE","CommandLine":"WINWORD.EXE /n invoice.docm"}}}
{"host":"ws1.lab.test","time":"2026-01-15T09:12:09Z","event":{"id":4688,"data":{"User":"LAB\\alice","Parent":"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE","Image":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","CommandLine":"powershell.exe -nop -w hidden -enc VwByAGkAdABlAC0ATwB1AHQAcAB1AHQAIAAnAGgAZQBsAGwAbwAgAGwAYQBiACcA"}}}
{"host":"ws1.lab.test","time":"2026-01-15T09:12:15Z","event":{"id":4688,"data":{"User":"LAB\\alice","Parent":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","Image":"C:\\Users\\alice\\AppData\\Local\\Temp\\updater.exe","CommandLine":"updater.exe --lab-marker"}}}
{"host":"ws2.lab.test","time":"2026-01-15T09:30:00Z","event":{"id":4688,"data":{"User":"LAB\\bob","Parent":"C:\\Windows\\explorer.exe","Image":"C:\\Windows\\System32\\notepad.exe","CommandLine":"notepad.exe notes.txt"}}}
{"host":"ws2.lab.test","time":"2026-01-15T09:31:00Z","event":{"id":4688,"data":{"User":"LAB\\bob","Parent":"C:\\Windows\\explorer.exe","Image":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","CommandLine":"powershell.exe -File C:\\Scripts\\inventory.ps1"}}}
'@

$events = $jsonl -split "`n" | Where-Object { $_.Trim() } | ForEach-Object { $_ | ConvertFrom-Json } |
    Select-Object host,
        @{n='Time';    e={ ([datetime]$_.time).ToUniversalTime() }},
        @{n='User';    e={ $_.event.data.User }},
        @{n='Parent';  e={ Split-Path -Leaf ($_.event.data.Parent -replace '\\', '/') }},
        @{n='Image';   e={ $_.event.data.Image }},
        @{n='Cmd';     e={ $_.event.data.CommandLine }}
"{0} events from {1} hosts" -f $events.Count, ($events.host | Sort-Object -Unique).Count

$office = 'WINWORD.EXE', 'EXCEL.EXE', 'POWERPNT.EXE', 'OUTLOOK.EXE'
foreach ($e in $events) {
    $leaf = Split-Path -Leaf ($e.Image -replace '\\', '/')
    if ($office -contains $e.Parent -and $leaf -match '^(powershell|pwsh|cmd|wscript|mshta)\.exe
  

) {
        "ALERT office-child   {0} {1:HH:mm:ss} {2} -> {3}" -f $e.host, $e.Time, $e.Parent, $leaf
    }
    if ($e.Cmd -match '\s-(e|en|enc|encodedcommand)\s+(?<b64>[A-Za-z0-9+/=]{16,})') {
        $decoded = [Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($Matches.b64))
        "ALERT encoded-ps     {0} {1:HH:mm:ss} decoded: {2}" -f $e.host, $e.Time, $decoded
    }
    if ($e.Image -match '\\(AppData|Temp|Users\\Public|ProgramData)\\') {
        "ALERT user-writable  {0} {1:HH:mm:ss} {2}" -f $e.host, $e.Time, $e.Image
    }
}
"== process tree on ws1"
$events | Where-Object host -eq 'ws1.lab.test' | Sort-Object Time |
    ForEach-Object { "{0:HH:mm:ss}  {1,-14} -> {2}" -f $_.Time, $_.Parent, (Split-Path -Leaf ($_.Image -replace '\\', '/')) }
5 events from 2 hosts
ALERT office-child   ws1.lab.test 09:12:09 WINWORD.EXE -> powershell.exe
ALERT encoded-ps     ws1.lab.test 09:12:09 decoded: Write-Output 'hello lab'
ALERT user-writable  ws1.lab.test 09:12:15 C:\Users\alice\AppData\Local\Temp\updater.exe
== process tree on ws1
09:12:00  explorer.exe   -> WINWORD.EXE
09:12:09  WINWORD.EXE    -> powershell.exe
09:12:15  powershell.exe -> updater.exe

The three alerts on ws1 are one chain: a macro document opened in Word started a hidden, encoded PowerShell, which started a program from the user's Temp folder. That is the textbook shape of a malicious-document infection (here with a harmless payload: the decoded command only prints a greeting). The PowerShell on ws2 runs a script file from C:\Scripts under Explorer and triggers nothing.

Logons from a CSV export

powershell · try it yourself
$csv = @'
TimeCreated,Id,TargetUserName,LogonType,IpAddress,WorkstationName
2026-01-15T08:59:00Z,4624,alice,2,-,WS1
2026-01-15T09:20:00Z,4624,alice,3,10.10.0.21,WS1
2026-01-15T09:40:00Z,4624,svc_backup,10,203.0.113.7,-
2026-01-15T09:41:00Z,4624,bob,2,-,WS2
2026-01-15T09:45:00Z,4624,svc_backup,3,10.10.0.40,BACKUP1
'@ | ConvertFrom-Csv

$names = @{ '2' = 'interactive'; '3' = 'network'; '10' = 'RDP' }
$csv | Group-Object LogonType | Sort-Object { [int]$_.Name } |
    ForEach-Object { "{0,-12} {1}" -f $names[$_.Name], $_.Count }
$csv | Where-Object { $_.TargetUserName -like 'svc_*' -and $_.LogonType -in '2', '10' } |
    ForEach-Object { "ALERT service account {0} interactive/RDP logon from {1}" -f $_.TargetUserName, $_.IpAddress }
$csv | Where-Object { $_.IpAddress -match '^\d' -and $_.IpAddress -notlike '10.10.0.*' } |
    ForEach-Object { "ALERT logon from outside the lab network: {0} {1}" -f $_.TargetUserName, $_.IpAddress }
interactive  2
network      2
RDP          1
ALERT service account svc_backup interactive/RDP logon from 203.0.113.7
ALERT logon from outside the lab network: svc_backup 203.0.113.7

Service accounts should log on as services or over the network, never by RDP; a remote-interactive logon by svc_backup from a documentation-range address is worth an incident ticket on its own.

How the analysis can mislead (detection of your own errors)

  • Missing fields are $null, not errors. A wrong property path such as $_.event.data.Cmdline (the field is CommandLine) silently yields nothing, and every rule on it stays quiet. Count the records where a key field is empty.
  • Time zones: ConvertFrom-Json turns ISO 8601 strings into [datetime] values, and formatting can show them in local time unless you call .ToUniversalTime(); mixing exports from collectors in different zones shifts events.
  • Command-line logging may be off. Without the policy, 4688 has an empty command line, and the encoded-PowerShell rule can never fire. Check that the field is populated before trusting a "no alerts" result.

How to make it reliable (prevention)

  1. Flatten once, into objects with clear property names, then write rules against those.
  2. Keep the raw record (or its line number) next to each alert, for the report.
  3. Turn on the telemetry first: command-line auditing for 4688, Sysmon with a maintained configuration, PowerShell Script Block Logging.
  4. Turn a hunt into a detection: the rules above are the kind of logic that becomes a Sigma rule in the blue-team course.

Pitfalls

  • Backslashes in JSON are escaped (\\); in a PowerShell regex, \\ matches one backslash.
  • **-split "n"** leaves \r` on Windows line endings; trim each line.
  • -match sets $Matches only when it succeeds; read it immediately inside the if.
  • Rules on file names alone are easy to dodge (rename powershell.exe); Sysmon's OriginalFileName and hashes are harder to fake.

Practice

1. Add a rule for -ExecutionPolicy Bypass or -ep bypass in a command line.

if ($e.Cmd -match '-(ep|exec|executionpolicy)\s+bypass') { … } with -match being case-insensitive by default. Bypass is common in admin scripts too, so combine it with the parent process or the user before alerting.

2. How would you build a parent → child tree for many hosts, when processes have IDs instead of names?

Use the process GUID or (host, process ID, start time) as the key, since IDs are reused: index events by ProcessGuid, then attach each event to its ParentProcessGuid. Sysmon provides both GUIDs for exactly this reason.

3. What does a 4624 with LogonType 3 from 10.10.0.40 for svc_backup most likely mean?

The backup server connecting to a share or service over the network, which is the expected behaviour for a service account. The RDP logon (type 10) from outside the lab network is the anomaly.

Test yourself

What chain made the three ws1 alerts suspicious together?

  1. Three logons
  2. Word started an encoded hidden PowerShell, which started a program from the user's Temp folder
  3. Notepad opened a file
  4. A scheduled script ran

Which encoding does [Text.Encoding]::Unicode decode, as needed for -EncodedCommand?

  1. UTF-8
  2. UTF-16LE
  3. ASCII
  4. Base64

The encoded-PowerShell rule finds nothing. Which check comes first before concluding all is clear?

  1. Restart the server
  2. Confirm that command-line logging is on, i.e. the CommandLine field is populated
  3. Delete old events
  4. Lower the threshold

Which logon type is RDP?

  1. 2
  2. 3
  3. 5
  4. 10