Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Script block logging records the PowerShell code that actually runs, after decoding and deobfuscation, as Event ID 4104. It is the single most valuable Windows log for detecting attacker tooling.

What Is PowerShell Script Block Logging?
PowerShell is installed on every Windows machine, signed by Microsoft, and capable of downloading and executing code entirely in memory. That combination has made it a favourite of intruders for a decade. Script block logging is Microsoft’s answer: it records the contents of each block of PowerShell code at the moment it is compiled for execution, after any encoding or obfuscation has been undone, and writes it to the Windows event log as Event ID 4104.
The deobfuscation is the important part. An attacker who launches powershell.exe -enc SQBFAFgA... leaves a command line that says nothing. Script block logging records what that Base64 decoded into, which is usually a complete attack tool.
Script block logging requires PowerShell 5.0 or later and is configured through Group Policy at Computer Configuration, Administrative Templates, Windows Components, Windows PowerShell, “Turn on PowerShell Script Block Logging”. It can equally be set through Intune or directly in the registry:
HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging
EnableScriptBlockLogging (REG_DWORD) = 1
EnableScriptBlockInvocationLogging = 0
Leave invocation logging disabled. It records the start and stop of every block execution and produces a volume of events that will swamp a collector for almost no additional detection value.
Events land in the Microsoft-Windows-PowerShell/Operational channel. The default channel size is far too small for a server that uses PowerShell for administration, so raise it and forward the events off the host. Long scripts are split across several 4104 events carrying MessageNumber and MessageTotal fields, and any detection logic needs to reassemble them before matching, or it will miss strings that straddle a boundary.
Two related features are worth enabling alongside it. Module logging (Event ID 4103) records pipeline execution details, and transcription writes a readable session transcript to a protected file share, which is invaluable when an investigation needs to see what an administrator or an attacker actually typed in sequence.
Attacker PowerShell has a recognisable vocabulary. Useful starting points for detection rules include:
FromBase64String, -EncodedCommand, and -enc in combination with long opaque strings.DownloadString, DownloadData, Invoke-Expression, IEX, Net.WebClient.Add-Type, System.Reflection.Assembly, VirtualAlloc, CreateThread, memset.Invoke-Mimikatz and sekurlsa, and direct access to lsass.-ExecutionPolicy Bypass, -NoProfile, -WindowStyle Hidden, -w hidden.Write these as portable detection logic rather than vendor queries where you can; the public Sigma rule repository already contains a large body of tested PowerShell rules that convert to most SIEM query languages.
Script block logging exists only in PowerShell 5 and later. An attacker who runs powershell.exe -Version 2 on a host where the PowerShell 2.0 engine is still present drops back to a runtime with no script block logging and no AMSI integration, and does so silently. Remove the Windows optional feature PowerShell 2.0 Engine across the estate, and alert on Event ID 400 showing EngineVersion=2 wherever it survives. Constrained language mode and application control through WDAC remove further avenues, though they take planning.
Script block logging captures everything the script contained, including any password, API key, or connection string typed or embedded in the code. Those events are then forwarded to a collector and retained for months, which converts a bad scripting habit into a durable credential exposure. Restrict read access to the operational channel, keep the SIEM index holding 4104 events under tighter access control than general logs, and treat the discovery of a plaintext secret in a script block as an incident requiring rotation. It is also worth noting that Windows logs blocks it considers suspicious at warning level even when the policy is disabled, so a handful of 4104 events appearing on an unconfigured host is expected behaviour rather than evidence the setting was on.
Script block logging pairs naturally with Sysmon process telemetry and the wider Windows event log set: Sysmon tells you a PowerShell process started and who its parent was, and 4104 tells you what it then did. At Privia Security, we tune this kind of Windows detection coverage as part of our SOC maturation services.
The performance cost is minor on normal workloads because logging happens when a block is compiled, not continuously. The practical cost is event volume on hosts that script heavily, such as management servers and build agents. Keep invocation logging off, raise the channel size, and filter noisy known-good scripts at collection rather than disabling the policy on those hosts.
Event ID 4688 records that a process started and, with command line auditing enabled, the command line used to start it. Event ID 4104 records the PowerShell code that ran inside that process, decoded and deobfuscated. An encoded command looks meaningless in 4688 and fully readable in 4104, which is why both are needed.
An attacker with administrative rights can change the registry setting or clear the channel, which is why forwarding events off the host matters more than the setting itself. More common is avoidance rather than disabling: downgrading to PowerShell 2.0, patching the logging provider in memory, or using a different scripting host entirely. Remove the version 2 engine and alert on configuration changes.
Yes. Workstations are where phishing payloads land and where most attacker PowerShell first runs, and they typically generate far fewer events than administrative servers. If volume is a concern, enable it everywhere, retain workstation events for a shorter period, and keep server events longer. Partial deployment creates exactly the blind spot an attacker needs.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.