Script block logging helps you see what PowerShell scripts are running on your system. It records the code blocks executed, so you can track and troubleshoot script activity.
Script block logging in PowerShell
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PowerShell
Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1
This command enables script block logging by setting a registry key.
You need administrator rights to run this command.
Examples
PowerShell
Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1
PowerShell
Get-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging'
PowerShell
Remove-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging'
Sample Program
This script enables script block logging and then confirms it is enabled by reading the registry key.
PowerShell
Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1 Get-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' | Select-Object EnableScriptBlockLogging
Important Notes
Script block logging records the full content of PowerShell scripts run on the system.
Logs are stored in the Windows Event Log under 'Microsoft-Windows-PowerShell/Operational'.
Enabling script block logging may slightly impact system performance due to extra logging.
Summary
Script block logging helps track PowerShell script execution for security and troubleshooting.
It is enabled by setting a registry key with PowerShell commands.
Logs appear in the Windows Event Log for review.
Practice
1. What is the main purpose of PowerShell script block logging?
easy
Solution
Step 1: Understand script block logging purpose
Script block logging records the commands run in PowerShell scripts to help track activity.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.Final Answer:
To record executed PowerShell commands for security and troubleshooting -> Option CQuick 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
Solution
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.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.Final Answer:
Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1 -> Option DQuick 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
Solution
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.Step 2: Match event log source
The correct source is 'Windows PowerShell', not general logs like Application, Security, or System.Final Answer:
Windows PowerShell -> Option AQuick 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
Solution
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.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.Final Answer:
The registry key was set under the wrong registry hive -> Option AQuick 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
Solution
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.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.Final Answer:
Modify the PowerShell profile script to log commands manually -> Option BQuick 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
