I have a simple .ps1
script that basically just creates a directory. When I right-click on the script in Windows Explorer and select “Run with PowerShell”, it gives the following error message:
Get-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of Unrestricted. Type “Get-ExecutionPolicy -List” to view your execution policy settings. For more information please see “Get-Help Set-ExecutionPolicy”.
(I recorded my screen and typed all those above, as the window only flashed quickly. Also, if I run “powershell -NoExit C:pathtoscript.ps1”, there is no error.
Interestingly, folder is still created.
However, if I open PowerShell myself and run the script like .script.ps1
, it does not have any error.
Here is the output of Get-ExecutionPolicy -List
:
Scope ExecutionPolicy
----- ---------------
MachinePolicy Unrestricted
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Unrestricted
I think “Run with PowerShell” is trying to do something (like setting execution policy) before running the script, but I cannot see the full command that it invoked, and I don’t know what scope it is trying to set execution policy for. Also, which of the above is more specific scope than which of the others?
I also don’t have admin access to override this context menu item either. How should I get a clean execution?
Might be different from Unable to execute PowerShell Script using ‘Run with Powershell’ option as it seems my execution actually succeeded.
I expect to run the script cleanly from Windows Explorer, i.e., there should be no error outputs.