Skip to content
Home » Server Hardening & Event Logs » PowerShell Script Block Logging: What It Is and How to Turn It On

PowerShell Script Block Logging: What It Is and How to Turn It On

An abstract digital illustration representing PowerShell Script Block Logging. A glowing magnifying glass examines lines of script code on a central display terminal. The code is connected by circuits to a secure, shield-shaped database icon with a padlock. Large bold text at the bottom reads "POWERSHELL SCRIPT BLOCK LOGGING: WHAT IT IS & WHY YOU NEED IT" with the 'prixl' logo in the bottom right corner.

PowerShell is the most capable tool built into every Windows server. Administrators use it to manage users, configure services and automate chores. Attackers use it for the same reasons: it is already installed, Windows trusts it, and it can download files, change settings and run code in memory without dropping a program on disk. MITRE lists it as a standard attacker technique (T1059.001). PowerShell script block logging is the setting that writes down what PowerShell actually ran, so that afterwards someone can see it.

This post covers what it records and where to find it, how to turn it on for Windows PowerShell and for PowerShell 7, the downgrade trick that skips it, and how to keep the log from overwriting itself.

What script block logging records

With the setting on, PowerShell records the text of every script block it processes, whether typed at a prompt, run from a script file, or passed in by another program. A long script is split across several events. Obfuscated commands are logged as PowerShell finally sees them, which often makes them readable.

Each record is event ID 4104. For Windows PowerShell 5.1, the version built into Windows, you will find them in Event Viewer under Applications and Services Logs > Microsoft > Windows > PowerShell > Operational. From PowerShell:

Get-WinEvent -FilterHashtable @{LogName='Microsoft-Windows-PowerShell/Operational'; Id=4104} -MaxEvents 20 | Format-List TimeCreated, LevelDisplayName, Message

You may already see some 4104 events at Warning level even though you never switched anything on. Since version 5, Windows PowerShell logs script blocks that contain terms commonly used in attacks automatically (Microsoft PowerShell team, 2015). That is a useful safety net, but it only catches what matches its list. Turning the policy on records everything.

Why it matters after an incident

Common PowerShell steps in real attacks include downloading a ransomware payload, pulling passwords out of memory, switching off Windows Defender and creating a hidden admin account. Without script block logging you may see the result (encrypted files, a new account) and have no record of the commands that caused it. Your IT provider cannot tell how far the attacker got, and you cannot tell which client data was touched, which is exactly what you need to know when deciding what to report to CERT-In within its 6-hour window.

With it on, the Operational log becomes a timeline of what was run. It complements process creation auditing (event 4688), which records that powershell.exe started; our audit policy baseline shows how to turn that on.

How to turn it on (Windows PowerShell 5.1)

Group Policy. Open gpedit.msc (or gpmc.msc in a domain) and go to Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell. Enable Turn on PowerShell Script Block Logging. Leave “Log script block invocation start / stop events” unticked; it adds a large number of extra events (4105 and 4106) for little gain.

Registry. The policy lives in the key HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging, as a DWORD value named EnableScriptBlockLogging set to 1. From an elevated Command Prompt:

reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1 /f

No restart is needed. Every new PowerShell session picks it up; sessions already open are not affected. To check the value:

reg query "HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging

PowerShell 7 has its own setting

If you or your IT vendor installed PowerShell 7 (pwsh.exe), the setting above does not cover it. PowerShell 7 reads a separate policy and writes to a separate log (Microsoft, about_Logging_Windows):

  • Registry: key HKLM\SOFTWARE\Policies\Microsoft\PowerShellCore\ScriptBlockLogging, DWORD EnableScriptBlockLogging = 1.
  • Group Policy: PowerShell 7 ships its own templates. Run InstallPSCorePolicyDefinitions.ps1 from the PowerShell 7 folder, then use Computer Configuration > Administrative Templates > PowerShell Core > Turn on PowerShell Script Block Logging. Each PowerShell Core setting has a “Use Windows PowerShell Policy setting” option, so you can make PowerShell 7 follow the Windows PowerShell setting instead of keeping two.
  • Where the events go: Applications and Services Logs > PowerShellCore > Operational, again as event 4104. If that log is missing, the event provider is not registered; run & "$PSHOME\RegisterManifest.ps1" from an elevated PowerShell 7 prompt.

For PowerShell 7 from a Command Prompt:

reg add "HKLM\SOFTWARE\Policies\Microsoft\PowerShellCore\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1 /f

Remove the PowerShell 2.0 engine

Script block logging arrived with PowerShell 5. The old 2.0 engine predates it, so on servers where 2.0 is still installed, an attacker can run powershell -Version 2 and nothing they type is recorded. Microsoft has now removed PowerShell 2.0 from Windows 11 24H2 and Windows Server 2025 (Microsoft, 2025), but older servers still carry it. Few business applications need it.

On Windows Server 2016, 2019 or 2022, from an elevated PowerShell window:

Get-WindowsFeature -Name PowerShell-V2
Uninstall-WindowsFeature -Name PowerShell-V2

On Windows 10, or Windows 11 before 24H2:

Disable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV2Root

Ask your IT vendor first if any old script or tool on the server was written for PowerShell 2.0.

Make the log big enough

The performance cost of script block logging is small. The log size is the real catch. The PowerShell Operational log defaults to 15 MB, and 4104 events are large (a long script fills several), so on a server where scripts run regularly, the log can overwrite itself within days. An attack noticed on Friday may have pushed Monday’s evidence out. Enlarge it, for example to 256 MB, from an elevated Command Prompt:

wevtutil sl "Microsoft-Windows-PowerShell/Operational" /ms:268435456

Do the same for PowerShellCore/Operational if you use PowerShell 7. CERT-In’s Directions of 28 April 2022 ask the organisations they cover to keep ICT system logs for a rolling 180 days in India; we recommend that as the target for your server logs.

Passwords in the log, and Protected Event Logging

Script block logging records scripts as written. If a script contains a password, for example a backup script that maps a drive with a hard-coded password, that password is now in the event log, readable by anyone who can read the log. Microsoft recommends Protected Event Logging when script block logging is used for anything beyond diagnostics. It encrypts the logged content with a certificate whose private key is kept somewhere else, so only the holder of that key can read it.

The setting is at Computer Configuration > Administrative Templates > Windows Components > Event Logging > Enable Protected Event Logging, and it needs a document-encryption certificate. For most small offices that is more machinery than they can maintain. The practical minimum is simpler: take passwords out of scripts (use Windows Credential Manager or a service account instead), and keep the number of people with admin rights on the server small, because they are the ones who can read the log.

What Prixl Warden does, and does not do, here

Warden’s security audit includes a check called “PS Script Block Logging”. It reads the Windows PowerShell 5.1 policy value above and flags the server if script block logging is not configured or is switched off. From the Hardening tab of the dashboard, a paid plan can switch it on in one click; it is listed as a safe fix, sets the same registry value shown above, and can be reverted.

Warden does not read the PowerShell event log or alert on PowerShell commands, and its check does not cover the separate PowerShell 7 setting. Script block logging is there so that you, your IT vendor or an incident responder have the evidence when it is needed. What Warden does watch and email you about are the events around such an attack: password-guessing, sign-ins outside office hours, new accounts and admin-group changes, new services and scheduled tasks, a cleared Security log, and the firewall being switched off.

To run the audit on your own server, start your 30-day free trial of Prixl Warden. The trial shows every check; the one-click fixes need a paid plan.

Related reading

Leave a Reply

Your email address will not be published. Required fields are marked *