PowerShell Script Won’t Run Without Confirmation

by Jul 11, 2023

When you right-click any PowerShell Script in Windows, the context menu sports the command “Run with PowerShell”. However, when you do, you may see a PowerShell console pop up asking weird questions about “Execution Policy”. Let’s say this up front: this has nothing to do with your personal execution policy settings that normally control whether a PowerShell script may run or not.

Instead, the context menu command runs its own code. You can look it up in the Windows registry:

$path = 'HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\0\Command'
$name = ''
$value = Get-ItemProperty -Path "Registry::$path" -Name $name 
$value.'(default)'

And this is what the context menu command runs whenever you call it:

if((Get-ExecutionPolicy ) -ne 'AllSigned') 
{ 
Set-ExecutionPolicy -Scope Process Bypass -Force 
}
& '%1'

So essentially, unless you are using the super strict execution policy “AllSigned” (which no one does), the execution policy is temporarily (just for this call) set to “Bypass” to allow you to run the script file that you right-clicked. Actually, the execution policy is loosened a bit so the context menu command works even on systems where no explicit execution policy was defined.

However, Set-ExecutionPolicy has the tendency to ask back to the user, and this is what can happen here and cause the innervating prompt. Users really don’t want to be asked “are you sure?” every time you are launching some script using this context menu command.

To fix the issue, simply adjust the code in said registry key. Add the -Force parameter to the call of Set-ExecutionPolicy so the cmdlet makes the adjustments without asking questions.