What if you could see every PowerShell command run on your system, instantly and clearly?
Why Script block logging in PowerShell? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are a system administrator trying to find out what commands were run on a server after a security incident. You have to manually check multiple logs and guess what scripts were executed.
This manual checking is slow and frustrating. Important script details might be missing or incomplete. You risk missing harmful commands or wasting hours piecing together clues.
Script block logging automatically records every script command run in PowerShell. It captures the exact code blocks executed, making it easy to review and audit all activity without guesswork.
Check event logs manually for script activitySet-PSDebug -Trace 1It enables quick, reliable tracking of all PowerShell script activity for security and troubleshooting.
After a suspicious login, you use script block logs to see exactly what commands the user ran, helping you stop a potential breach fast.
Manual log checks are slow and error-prone.
Script block logging captures all executed script code automatically.
This improves security audits and troubleshooting speed.
Practice
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]
- Confusing logging with script optimization
- Thinking it encrypts scripts
- Assuming it auto-fixes errors
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]
- Using non-existent cmdlets like Enable-ScriptBlockLogging
- Incorrect registry paths
- Confusing execution policy with logging
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]
- Looking in Application or System logs
- Confusing Security log with script block logging
- Not knowing event log sources
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]
- Setting keys under HKCU instead of HKLM
- Assuming execution policy blocks logging
- Ignoring event log service status
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]
- 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
