0
votes

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
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"Olaf

1 Answers

2
votes

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