#Why this matters
PowerShell sits directly on top of .NET, which gives administrators full access to Windows Management Instrumentation (WMI), the Registry, services and network sockets. Because powershell.exe is a Microsoft-signed binary present on every modern Windows host, adversaries also use it for fileless execution in memory without dropping a compiled executable to disk.
If your team only checks files on disk or treats Execution Policy as a lock, obfuscated commands run unseen. Effective Windows defence combines real runtime restrictions, in-memory scanning via AMSI, and full PowerShell logging.
#Execution Policies versus real boundaries
PowerShell's Execution Policy controls when PowerShell loads configuration files and .ps1 scripts. Common modes include Restricted (no .ps1 files), AllSigned (signed scripts only) and RemoteSigned (downloaded scripts must be signed). You can check the active scopes on a host with Get-ExecutionPolicy -List.
Microsoft explicitly states that Execution Policy is not a security boundary. A user who can open a terminal can still pass commands via -Command, pipe code into Invoke-Expression, encode a command with -EncodedCommand, or launch a one-off session with -ExecutionPolicy Bypass.
To enforce an actual security boundary, pair AppLocker or Windows Defender Application Control (WDAC) with PowerShell Constrained Language Mode (ConstrainedLanguage). Under Constrained Language Mode, core cmdlets still work for approved admin scripts, while direct instantiation of arbitrary .NET classes, COM objects and Win32 API calls is blocked.
| Control | Purpose | Stops intentional bypass? |
|---|---|---|
Execution Policy (Restricted, AllSigned) |
Prevents accidental .ps1 runs |
No |
| AppLocker / WDAC | Allows only signed or approved scripts and binaries | Yes |
| Constrained Language Mode | Restricts dangerous .NET and Win32 API access | Yes, when enforced by WDAC/AppLocker |
#How AMSI inspects scripts in memory
Attackers routinely hide PowerShell commands using Base64 (-EncodedCommand, which uses UTF-16LE encoding), string concatenation or variable substitution so static disk scanners see nothing suspicious.
The Antimalware Scan Interface (AMSI) closes that gap at runtime. Right before PowerShell parses and executes a script block, every obfuscation layer has already been unwrapped into plain text inside System.Management.Automation. PowerShell passes that decoded buffer through amsi.dll (AmsiScanBuffer) to the registered security engine (such as Microsoft Defender Antivirus) before execution proceeds.
Because tampering with AMSI in memory is a known adversary technique, defenders rely on layered telemetry (especially Script Block Logging and EDR behavioural alerts) rather than any single sensor.
#PowerShell logging: 4103, 4104 and transcription
Windows provides three complementary PowerShell audit layers configurable through Group Policy (Windows Components > Windows PowerShell) or HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell:
- Module Logging (Event ID 4103): logs pipeline execution events and parameter bindings for selected modules (or
*for all modules) inMicrosoft-Windows-PowerShell/Operational. - Script Block Logging (Event ID 4104): records the full de-obfuscated text of every script block as the engine evaluates it. Even if an attacker uses
Invoke-Expressionor nested Base64 layers, each decoded stage appears in Event ID 4104. (Windows logs blocks matching built-in suspicious patterns at Warning level by default, and logs every block when fully enabled.) - Transcription Logging: writes plain-text transcripts of console input, output and timestamps to a designated directory, ideally a central write-only share.
$ powershell.exe -NoProfile -EncodedCommand VwByAGkAdABlAC0ATwB1AHQAcAB1AHQAIAAiAGwAYQBiAC0AYwBoAGUAYwBrACIA
lab-check
$ powershell.exe -NoProfile -Command "Get-WinEvent -FilterHashtable @{LogName='Microsoft-Windows-PowerShell/Operational'; Id=4104} -MaxEvents 1 | Select-Object TimeCreated, Message"
TimeCreated Message
----------- -------
11/19/2026 5:12:04 AM Creating Scriptblock text (1 of 1):
Write-Output "lab-check"Notice how -EncodedCommand used UTF-16LE Base64 (VwBy...), yet Event ID 4104 recorded the exact decoded command Write-Output "lab-check".
#Defending and common mistakes
- Remove or block PowerShell v2: PowerShell 2.0 (
powershell.exe -Version 2) has no AMSI and no Script Block Logging. Uninstall theMicrosoftWindowsPowerShellV2optional feature so attackers cannot downgrade into a blind engine. - Audit PowerShell 7 (
pwsh.exe) too: modern environments often install PowerShell 7 alongside Windows PowerShell 5.1; configure its separate Group Policy andpwshevent channel as well. - Common mistake - treating
AllSignedas malware protection: attackers skip.ps1files entirely with-Commandor-EncodedCommand. - Common mistake - storing transcripts locally: if transcription writes only to a local folder, an elevated intruder can delete the files before detection. Forward logs off-host and write transcripts to a protected central share.
#Summary
- Execution policies prevent accidental script execution, whereas real enforcement requires AppLocker/WDAC and Constrained Language Mode.
- AMSI inspects de-obfuscated script buffers in memory through
amsi.dllright before execution. - Module Logging (4103), Script Block Logging (4104) and Transcription capture decoded script blocks and console output even when commands are Base64-encoded.
- Removing PowerShell v2 and forwarding logs off-host closes common downgrade and tampering blind spots.