Script block logging in PowerShell - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to log script blocks grows as more scripts run.
How does logging affect performance when many script blocks execute?
Analyze the time complexity of the following code snippet.
Enable-PSRemoting -Force
Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging' -Name 'EnableScriptBlockLogging' -Value 1
for ($i = 0; $i -lt $n; $i++) {
Invoke-Command -ComputerName Server01 -ScriptBlock {
Write-Output "Running iteration $using:i"
}
}
This code enables script block logging and uses a loop to invoke a remote script block that outputs text multiple times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that invokes the remote script block $n times.
- How many times: $n times (one script block invocation per loop iteration).
Each iteration logs a script block action, so the logging work grows with the number of iterations.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log entries |
| 100 | 100 log entries |
| 1000 | 1000 log entries |
Pattern observation: The logging work increases directly with the number of loop iterations.
Time Complexity: O(n)
This means the time to log grows in a straight line as the number of script block executions increases.
[X] Wrong: "Logging script blocks happens instantly and does not affect performance."
[OK] Correct: Each logged script block adds work, so more script blocks mean more time spent logging.
Understanding how logging impacts script execution helps you balance security and performance in real environments.
"What if we batch multiple script block executions before logging? How would the time complexity change?"
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
