PowerShell Genie

Audit PowerShell script block logging

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Read the PowerShell ScriptBlockLogging policy registry key with Get-ItemProperty and interpret missing settings before drawing conclusions.

The script

1PS> Get-ItemProperty "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" -ErrorAction SilentlyContinue

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Contoso
    2 InstallPath : C:\Program Files\Contoso
    3 Version : 4.2.1
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-ItemProperty "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" -ErrorAction SilentlyContinue
    1. Stage 1Get-ItemProperty-ErrorAction

      How it works

      Get-ItemProperty reads the ScriptBlockLogging policy key and returns whatever values it holds, with -EA SilentlyContinue keeping the command quiet on hosts where the key does not exist because the policy was never applied.

      Adapting it

      Test the EnableScriptBlockLogging value explicitly against 1 rather than dumping the whole key, so the result becomes a boolean a monitoring system can alert on.

      If the result is unexpected

      Script-block logging appears enabled but expected events are absent.

      Effective policy, the PowerShell edition, log channel and execution time influence available evidence.

      1. Check policy for the host that ran the command and inspect the matching operational log around that time.

      Validate the result

      A controlled test produces an event in the intended channel or the missing prerequisite is identified.

      Keep the check reliable

      Document the edition and logging channel and account for sensitive data in retained script content.

      Primary documentation

      Windows services and processes documentation

      Continue
      Genie guideAudit PowerShell script block logging

      Read the PowerShell ScriptBlockLogging policy registry key with Get-ItemProperty and interpret missing settings before drawing conclusions.