L9. Security: Execution Policy, Logging, AMSI, and Secrets
Course outlineLesson 9 of 10
Learn what PowerShell security controls do and do not do: execution policy, script block logging, module logging, AMSI, constrained language mode, application control, and secret handling.
Execution Policy Is Not a Security Boundary
Execution policy helps prevent accidental script execution, but Microsoft explicitly documents that it is not a security system that restricts user actions. On Windows, policies such as RemoteSigned, AllSigned, and Restricted affect when scripts and configuration files load. On non-Windows systems, execution policy behavior is effectively unrestricted/bypass-like because Windows security zones are not implemented.
Get-ExecutionPolicy
Get-ExecutionPolicy -List
Set-ExecutionPolicy RemoteSigned -Scope CurrentUserOn Windows the precedence is MachinePolicy, UserPolicy, Process, CurrentUser, then LocalMachine. Group Policy can override local settings. Always check Get-ExecutionPolicy -List before assuming what policy is effective.
Logging
PowerShell security monitoring relies heavily on logs:
| Control | What it records |
|---|---|
| Module logging | Pipeline execution events for selected modules |
| Script block logging | Commands, functions, scripts, and script blocks |
| Transcription | Console input and output for a session |
For PowerShell 7 on Windows, enabled script block logging writes event 4104 to PowerShellCore/Operational. Windows PowerShell 5.1 uses Microsoft-Windows-PowerShell/Operational. Configure logging for the edition you actually run, execute a harmless script block, and confirm receipt. Module logging must cover the intended modules. Transcription records a session transcript but is not a substitute for script block telemetry.
Logs and transcripts can expose passwords or tokens. Restrict access and retention; use Protected Event Logging where appropriate.
AMSI and Language Restrictions
On supported Windows systems, PowerShell sends script content to AMSI for antimalware inspection. PowerShell 7.3 extended AMSI data to include .NET method invocations. Constrained Language Mode restricts .NET types and language features. Enforce it with a system application control policy such as App Control for Business or AppLocker. Setting $ExecutionContext.SessionState.LanguageMode manually is useful for testing, but does not establish a security boundary.
Application control is the stronger enterprise control. Microsoft identifies App Control for Business as the preferred Windows application control system. Execution policy remains defense-in-depth, not containment.
Secrets
Do not hardcode passwords, tokens, or API keys in scripts. Prefer managed identities, certificates, environment-specific secret stores, or SecretManagement-compatible vaults. Microsoft no longer recommends SecureString for new development as a general password-handling strategy; it remains for compatibility and reducing accidental console/log exposure.
Lab
- Run
Get-ExecutionPolicy -List. - Explain which scope wins by precedence.
- After enabling script block logging through an approved policy, run a harmless command and verify event 4104 in the operational channel for your PowerShell edition.
- Rewrite a sample script so it accepts a token from a secret store or environment variable instead of hardcoding it.
References
- Execution policy and precedence
- PowerShell security features
- Windows logging
- Language modes and enforcement
- Windows PowerShell 5.1 logging
Continue Learning
Apply the controls in the capstone- ✓Execution policy is a safety feature, not a security boundary
- ✓Execution policies are enforced only on Windows platforms
- ✓Group Policy scopes take precedence over local execution policy settings
- ✓Script block logging records commands, functions, scripts, and script blocks for investigation
- ✓AMSI and application control provide stronger defensive value than execution policy alone
1. What is the correct security interpretation of PowerShell execution policy?
2. Which logging feature records commands and script blocks processed by PowerShell?