Bird
Raised Fist0
PowerShellscripting~10 mins

Script block logging in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Script block logging
Enable Script Block Logging
PowerShell runs script block
Script block content captured
Log entry created in Event Log
Admin reviews logs for auditing
This flow shows how enabling script block logging captures PowerShell script blocks as they run and records them in event logs for auditing.
Execution Sample
PowerShell
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 1
Write-Output "Hello, World!"
This code enables script block logging via registry and runs a simple PowerShell command that will be logged.
Execution Table
StepActionEvaluationResult
1Set registry key to enable script block loggingRegistry key set successfullyScript block logging enabled
2Run Write-Output commandCommand executesOutput: Hello, World!
3PowerShell captures script block contentScript block content: Write-Output "Hello, World!"Content logged to event log
4Event log entry createdLog contains script block text and metadataLog entry available for admin review
5Admin checks event logLog entry found with script block detailsAuditing successful
💡 Script block logging enabled and script block content successfully logged in event log
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
EnableScriptBlockLoggingNot set1 (enabled)1 (enabled)1 (enabled)1 (enabled)
ScriptBlockContentNoneNoneWrite-Output "Hello, World!"Write-Output "Hello, World!"Logged in event log
Key Moments - 3 Insights
Why doesn't script block logging capture commands before enabling it?
Script block logging only captures script blocks executed after the registry key is set, as shown in step 1 enabling logging before running commands in step 2.
Does script block logging affect the output of commands?
No, as seen in step 2, the command outputs normally while logging happens in the background (step 3 and 4).
Where can I find the logged script block content?
The logged content is stored in the Windows Event Log under PowerShell operational logs, confirmed in step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of EnableScriptBlockLogging after step 1?
A1 (enabled)
B0 (disabled)
CNot set
DError
💡 Hint
Check variable_tracker row for EnableScriptBlockLogging after Step 1
At which step does PowerShell capture the script block content?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table rows describing script block capture
If script block logging was not enabled, what would change in the execution table?
AStep 4 would create a log entry anyway
BStep 2 would fail to run command
CStep 3 would not capture script block content
DStep 5 would show auditing successful
💡 Hint
Refer to the exit_note and step 1 enabling logging
Concept Snapshot
Script block logging captures PowerShell script blocks as they run.
Enable it by setting registry key EnableScriptBlockLogging to 1.
Logged script blocks appear in Windows Event Logs.
Does not affect command output.
Useful for auditing and security monitoring.
Full Transcript
Script block logging in PowerShell works by enabling a registry key that tells PowerShell to record every script block it runs. When enabled, each script block's content is captured and saved in the Windows Event Log. This helps administrators audit what scripts have run on a system. The process starts by setting the registry key EnableScriptBlockLogging to 1. After that, any PowerShell command or script block executed is logged without changing its output. The logs can be reviewed later in the event viewer under PowerShell operational logs. This method is useful for security and troubleshooting.

Practice

(1/5)
1. What is the main purpose of PowerShell script block logging?
easy
A. To automatically fix errors in scripts
B. To speed up script execution by caching commands
C. To record executed PowerShell commands for security and troubleshooting
D. To encrypt PowerShell scripts for protection

Solution

  1. Step 1: Understand script block logging purpose

    Script block logging records the commands run in PowerShell scripts to help track activity.
  2. Step 2: Compare options to purpose

    Only "To record executed PowerShell commands for security and troubleshooting" matches the purpose of recording commands for security and troubleshooting.
  3. Final Answer:

    To record executed PowerShell commands for security and troubleshooting -> Option C
  4. Quick Check:

    Script block logging = record commands [OK]
Hint: Remember: logging means recording actions, not speeding or fixing [OK]
Common Mistakes:
  • Confusing logging with script optimization
  • Thinking it encrypts scripts
  • Assuming it auto-fixes errors
2. Which PowerShell command correctly enables script block logging by setting the registry key?
easy
A. Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy ScriptBlockLogging
B. Enable-ScriptBlockLogging -Value 1
C. New-Item -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell' -Name 'ScriptBlockLogging' -Value 1
D. Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1

Solution

  1. Step 1: Identify correct registry path and property

    The registry path for script block logging is under HKLM\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging with property EnableScriptBlockLogging.
  2. Step 2: Match command syntax

    Set-ItemProperty sets a registry value correctly. Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1 uses correct path, property, and value 1 to enable logging.
  3. Final Answer:

    Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1 -> Option D
  4. Quick Check:

    Set-ItemProperty + correct path = enable logging [OK]
Hint: Use Set-ItemProperty with full registry path to enable logging [OK]
Common Mistakes:
  • Using non-existent cmdlets like Enable-ScriptBlockLogging
  • Incorrect registry paths
  • Confusing execution policy with logging
3. Given the registry key is set to enable script block logging, what event log source will you check to see logged script blocks?
medium
A. Windows PowerShell
B. Application
C. Security
D. System

Solution

  1. Step 1: Identify where PowerShell logs script block events

    PowerShell script block logging events appear in the Windows PowerShell event log under Applications and Services Logs.
  2. Step 2: Match event log source

    The correct source is 'Windows PowerShell', not general logs like Application, Security, or System.
  3. Final Answer:

    Windows PowerShell -> Option A
  4. Quick Check:

    Script block logs appear in Windows PowerShell log [OK]
Hint: Check 'Windows PowerShell' log for script block events [OK]
Common Mistakes:
  • Looking in Application or System logs
  • Confusing Security log with script block logging
  • Not knowing event log sources
4. You enabled script block logging but no events appear in the Windows PowerShell log. What is a likely cause?
medium
A. The registry key was set under the wrong registry hive
B. PowerShell script execution is disabled
C. The event log service is stopped
D. The script block logging feature is only for PowerShell 5.0 and above

Solution

  1. Step 1: Check registry hive correctness

    Script block logging requires setting the key under HKLM (local machine). Setting it under HKCU or wrong hive causes no logging.
  2. Step 2: Evaluate other options

    PowerShell execution policy does not block logging; event log service stopping would affect all logs; script block logging works in PowerShell 5.0+ but question assumes correct version.
  3. Final Answer:

    The registry key was set under the wrong registry hive -> Option A
  4. Quick Check:

    Wrong registry hive = no logs [OK]
Hint: Always set registry keys under HKLM for script block logging [OK]
Common Mistakes:
  • Setting keys under HKCU instead of HKLM
  • Assuming execution policy blocks logging
  • Ignoring event log service status
5. You want to enable script block logging only for scripts running under a specific user account without affecting others. Which approach is best?
hard
A. Set the EnableScriptBlockLogging registry key under HKLM for all users
B. Modify the PowerShell profile script to log commands manually
C. Use Group Policy to enable script block logging for all users
D. Set the EnableScriptBlockLogging registry key under HKCU for that user

Solution

  1. Step 1: Understand scope of script block logging

    Built-in script block logging is a machine-wide feature configured under HKLM or Group Policy, affecting all users.
  2. Step 2: Identify per-user alternative

    HKCU does not enable script block logging (as it requires HKLM). Modifying the user's PowerShell profile to manually log commands (e.g., Start-Transcript) achieves per-user logging without affecting others.
  3. Final Answer:

    Modify the PowerShell profile script to log commands manually -> Option B
  4. Quick Check:

    Per-user logging = profile script [OK]
Hint: Use PowerShell profile for per-user command logging [OK]
Common Mistakes:
  • Using HKLM or Group Policy which affects all users
  • Setting HKCU key (does not enable built-in logging)
  • Assuming built-in logging supports per-user config