Event ID 4104: Script block logging
Records the full text of PowerShell script blocks as they are compiled for execution, after de-obfuscation. The single most valuable PowerShell event for IR.
- Channel
- Microsoft-Windows-PowerShell/Operational
- Logged by default
- Varies by version
- Written on
- Any host
- Volume
- High
- Provider
- Microsoft-Windows-PowerShell
- Needs a SACL
- No
To have this event
- Full coverage: GPO Turn on PowerShell Script Block Logging, or registry HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging\EnableScriptBlockLogging = 1
- Suspicious blocks are recorded as Warning events even without the policy on PowerShell 5+
Key fields
Exact XML field names, as they appear in <Data Name="…"> and in SIEM field extractions.
| Field | What it tells you |
|---|---|
| ScriptBlockText | The actual code that ran, de-obfuscated. This is where encoded, compressed or string-concatenated payloads are revealed in cleartext |
| ScriptBlockId | GUID identifying the script block; the same block run repeatedly shares this ID |
| Path | Path of the script file when the block came from a file; empty for interactive or in-memory code |
| MessageNumber | Sequence number of this fragment when a long script block is split across multiple events |
| MessageTotal | Total number of fragments for this script block; reassemble all parts to recover the whole script |
Analyst notes
- 01Even without any policy, PowerShell 5 logs script blocks it judges suspicious as Warning-level (level 3) 4104 events, so some coverage exists by default; full coverage needs script block logging enabled.
- 02Long or obfuscated payloads are split: a block with MessageTotal > 1 must be reassembled in MessageNumber order to read the whole thing.
- 03Common attack strings to hunt: FromBase64String, IEX/Invoke-Expression, DownloadString, -enc, [Reflection.Assembly]::Load, Add-Type, and AMSI-bypass patterns.
- 04Attackers evade this by downgrading to PowerShell 2.0, where script block logging does not exist; watch Windows PowerShell 400 for EngineVersion 2.0.
Pull this event
Retrieval queries for a live host, a collected .evtx file and common SIEMs. Filter further on the key fields above.
Get-WinEvent -LogName 'Microsoft-Windows-PowerShell/Operational' -FilterXPath "*[System[(EventID=4104)]]" -MaxEvents 50 |
Select-Object TimeCreated, Id, MachineName, Message-FilterXPath supports exact matches only. For substring matching, filter in Where-Object.
Where it shows up
Investigations, attack signals and lateral movement techniques that rely on this event.
- Lateral movementWinRM and PowerShell RemotingDestination: Script block logging captures the remote PowerShell code after de-obfuscation, when enabled.
- InvestigationWhat did PowerShell run on this host?Pull script block logging first
- InvestigationWhen did the attacker first get in? Building the initial access timelineFind the first execution
ATT&CK mapping
Techniques this event can help detect. Most of these depend on specific field values; the event alone does not mean the technique happened.
Also relevant, depending on field values
Related events
Common questions
- What does Windows event ID 4104 mean?
- Records the full text of PowerShell script blocks as they are compiled for execution, after de-obfuscation. The single most valuable PowerShell event for IR. It is written to the PowerShell Operational log (Microsoft-Windows-PowerShell/Operational).
- Is event 4104 logged by default?
- No. It must be enabled before it is recorded.
- Where is event 4104 recorded?
- In the Microsoft-Windows-PowerShell/Operational channel on the any host.
Decode a real event
Paste the XML and every value gets explained. Nothing leaves your browser.
Open decoderRecords the full text of PowerShell script blocks as they are compiled for execution, after de-obfuscation. The single most valuable PowerShell event for IR. Key fields, logging requirements, ATT&CK mapping and ready-to-run queries.