magazine

September 28, 2022 7:35 AM
logo
Power shell

If you’re getting an error when trying to run PowerShell scripts, it’s likely because your PowerShell execution policy is set to something other than “Unrestricted”.

In this article, we’ll show you how to fix that in 6 ways:

1) Open PowerShell as an administrator.

To do this, press the Windows key + X, then select “Windows PowerShell (Admin)”.

2) Type the following command and press Enter:

Set-Execution Policy Unrestricted

3) You’ll see a message that says “Execution Policy Change,” followed by “The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?”

4) Press Y for yes, and then Enter.

5) Close PowerShell and try running your script again.

If that didn’t work, try one of the following methods:

Method 1: Run PowerShell as an administrator

1) Press the Windows key + X, then select “Windows PowerShell (Admin)”.

2) Type the following command and press Enter:

Set-Execution Policy Unrestricted

3) You’ll see a message that says “Execution Policy Change,” followed by “The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?”

4) Press Y for yes, and then Enter.

5) Close PowerShell and try running your script again.

Method 2: Set the execution policy for all users

1) Press the Windows key + R, type regedit, and press Enter.

2) In the Registry Editor, navigate to the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

3) Right-click Microsoft. PowerShell, then click New > DWORD (32-bit) Value.

4) Name the new value Execution Policy, then double-click it and set its value data to Unrestricted.

5) Click OK, and then close the Registry Editor and try running your PowerShell script again.

Method 3: Set the execution policy for the current user

1) Press the Windows key + R, type regedit, and press Enter.

2) In the Registry Editor, navigate to the following key:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

3) Right-click Microsoft. PowerShell, then click New > DWORD (32-bit) Value.

4) Name the new value Execution Policy, then double-click it and set its value data to Unrestricted.

5) Click OK, and then close the Registry Editor and try running your PowerShell script again.

Method 4: Bypass the execution policy

1) Open PowerShell as an administrator.

To do this, press the Windows key + X, then select “Windows PowerShell (Admin)”.

2) Type the following command and press Enter:

PowerShell -execution policy bypass -file C:\path\to\script.ps1

3) Replace C:\path\to\script.ps1 with the path to your PowerShell script.

4) Close PowerShell and try running your script again.

Method 5: Allow all scripts to run

1) Open PowerShell as an administrator.

To do this, press the Windows key + X, then select “Windows PowerShell (Admin)”.

2) Type the following command and press Enter:

Get-Execution Policy -List

3) You should see a list of execution policies, similar to the one below:

  • Scope Execution Policy
  • Machine Policy Undefined
  • User Policy Undefined
  • Process Restricted
  • Current User Unrestricted <– This is the one you want to change
  • Local Machine Unrestricted

4) Type the following command and press Enter:

Set-Execution Policy -Scope Current User Unrestricted -Force

5) Close PowerShell and try running your script again.

Method 6: Set the execution policy for all users using Group Policy Editor (GPEdit.msc)

1) Press the Windows key + R, type gpedit.msc, and press Enter.

2) In the Local Group Policy Editor, navigate to the following policy setting:

Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell

3) Double-click Turn on Script Execution.

4) Select Enable in the left column, and then click OK at the bottom of the window.

5) Close the Local Group Policy Editor and try running your script again.

Conclusion:

There are a few different ways to change the PowerShell execution policy. The best way is to set it for all users using Group Policy Editor (GPEdit.msc). This will allow you to run PowerShell scripts without having to change the execution policy each time. If you can’t use Group Policy Editor, then you can try one of the other methods listed above. You can check RemoteDBA for more information.

There are a few different ways to change the PowerShell execution policy so that you can run your scripts. Try each of the methods until you find one that works for you.

 

Leave a Reply

Your email address will not be published.