Andrew
Andrew

Reputation: 1575

Run PowerShell script with admin privileges and bypass execution policy

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.

Upvotes: 1

Views: 20396

Answers (2)

Dan Johnson
Dan Johnson

Reputation: 1

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.

Upvotes: 0

m0lochwalker
m0lochwalker

Reputation: 432

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

Upvotes: 5

Related Questions