Why are my PowerShell scripts not running?

Viewed 213617

I wrote a simple batch file as a PowerShell script, and I am getting errors when they run.

It's in a scripts directory in my path. This is the error I get:

Cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about-signing".

I looked in the help, but it's less than helpful.

10 Answers

It could be PowerShell's default security level, which (IIRC) will only run signed scripts.

Try typing this:

set-executionpolicy remotesigned

That will tell PowerShell to allow local (that is, on a local drive) unsigned scripts to run.

Then try executing your script again.

You need to run Set-ExecutionPolicy:

Set-ExecutionPolicy Restricted <-- Will not allow any powershell scripts to run.  Only individual commands may be run.

Set-ExecutionPolicy AllSigned <-- Will allow signed powershell scripts to run.

Set-ExecutionPolicy RemoteSigned <-- Allows unsigned local script and signed remote powershell scripts to run.

Set-ExecutionPolicy Unrestricted <-- Will allow unsigned powershell scripts to run.  Warns before running downloaded scripts.

Set-ExecutionPolicy Bypass <-- Nothing is blocked and there are no warnings or prompts.

Also it's worth knowing that you may need to include .\ in front of the script name. For example:

.\scriptname.ps1

We can bypass execution policy in a nice way (inside command prompt):

type file.ps1 | powershell -command -

Or inside powershell:

gc file.ps1|powershell -c -

On Windows 10: Click change security property of myfile.ps1 and change "allow access" by right click / properties on myfile.ps1

It would be ideal to bypass execution policies e.g. through

powershell -executionpolicy bypass -File .\MYSCRIPT.ps1

Unfortunately this can still be prevented by group policies. As a workaround, you can encode your script as Base64 by running this in PowerShell:

[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes((Get-Content .\MYSCRIPT.ps1)))

Then execute the result like this:

powershell.exe -EncodedCommand "put-your-base64-string-here"

Caveat: This won't work with scripts that require parameters.

Related