1

I'm having trouble with PowerShell script. I'm getting an error when running it

cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

Script is not being run as admin.

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    $arguments = "& '" + $MyInvocation.MyCommand.Definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    break
}

Set-ExecutionPolicy Bypass -Scope Process -Force

# Rest code

Seems like the line with execution policy is not being executed.

The command

Set-ExecutionPolicy Unrestricted

didn't help as well.

1
  • 3
    You cannot bypass the execution policy from inside a script. You cannot run this script because of the execution policy. You can call the Powershell executable with the according parameter like this: Powershell.exe -ExecutionPolicy ByPass -File "Your file name here" Commented Feb 9, 2019 at 16:04

2 Answers 2

5

The ExecutionPolicy is keeping the script from running at all. You will have to call the .ps1 with parameters that deal with that up front.

Powershell.exe -ExecutionPolicy Bypass -File yourscript.ps1

Sign up to request clarification or add additional context in comments.

Comments

0

I have struggled with this. I have found that if I type in the double quotes in the task schedule action tab, it will work. If you copy and paste, the double quote is different and does not work. If you type a quote in Word for example, it looks like this “, if you type it in Task Scheduler it looks like this ". Same exact key on the keyboard, but apparently different ascii code and it does not work when you copy and paste.

2 Comments

It should be self-evident that Microsoft Word is not a programming environment.
This may be a problem, but does not appear to be OP's problem - see user Olaf's comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.