How to change execution policy to run scripts on PowerShell

  • To change the PowerShell execution policy, open PowerShell (admin) and run the “Set-ExecutionPolicy RemoteSigned” command to set execution to allow scripts. The “Set-ExecutionPolicy AllSigned” command allows scripts from trusted publishers. Or the “Set-ExecutionPolicy Unrestricted” command allows scripts without restrictions.
  • You can also run the “Set-ExecutionPolicy Restricted” to undo the changes and prevent scripts from running.
  • Finally, you can allow PowerShell scripts by changing the policy from the Windows 11 and 10 Settings app.

On Windows 11 (or 10), by default, when you try to run a script on PowerShell, it will fail with the “cannot be loaded because running scripts is disabled on this system” error message. This is because PowerShell has an “execution policy” security feature that controls how to load configuration files and run scripts to prevent malicious code from running on the system.

If you must run a script on Windows 11 (or 10), you can change the execution policy for the local computer, current user, or session.

When you set a PowerShell execution policy for the local computer and current user, the information is stored in the Registry. If you configure the policy for a particular session, it is held in memory and then lost when you close the session.

According to Microsoft, the execution policy doesn’t restrict actions. You can bypass the policy by typing the script in the command-line interface (CLI). The policy has been designed to help users prevent running malicious scripts.

In this guide, I will teach you how to change the execution policy to run scripts successfully on PowerShell on Windows 11 or 10.

Change execution policy on Windows 11 from PowerShell

To change the PowerShell execution policy on Windows 11 (or 10), use these steps:

  1. Open Start on Windows 11.

  2. Search for PowerShell, right-click the top result, and select the Run as administrator option.

  3. Type the following command to confirm the current execution policy and press Enter:

    Get-ExecutionPolicy
  4. (Optional) Type the following command to view the effective PowerShell execution policies and press Enter:

    Get-ExecutionPolicy -List
  5. Type the following command to change the PowerShell execution policy to allow scripts to run on Windows 11 and press Enter:

    Set-ExecutionPolicy RemoteSigned
  6. (Optional) Type the following command to change the execution policy to prevent scripts from running and press Enter:

    Set-ExecutionPolicy Restricted

Once you complete the steps, you can execute the script again, and it should now run successfully on Windows 11. If you no longer want to allow scripts to run on your computer, you can run the optional command mentioned in the steps.

If you want to change the execution policy to run scripts for the current user or local computer, you need to use the “Scope” option like this: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser or Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine.

On Windows 11, you can choose from four different execution policies to allow or deny scripts:

  • Restricted — blocks any script file from running.
  • RemoteSigned — allows scripts to be created on the computer. However, scripts created on another device won’t run without a trusted signature.
  • AllSigned — allows all scripts to run. However, only if a trusted publisher has included a signature.
  • Unrestricted — runs any script without restrictions.

You don’t need to change the execution policy if you need to run a script that does not have the required parameters and does not return output. Instead, you can use the PowerShell.exe -File "FILENAME" -ExecutionPolicy Bypass command to bypass the restrictions.

Change execution policy on Windows 11 from Settings

On Windows 11 and 10, you can also allow scripts to run on PowerShell by changing the configuration of the command console from the Settings app.

From Windows 11

To change the PowerShell execution policy from the Settings app, use these steps:

  1. Open Settings.

  2. Click on System.

  3. Click the For developers page.

  4. Click the PowerShell setting.

  5. Turn on the “Change execution policy to allow local PowerShell scripts […]” toggle switch.

If you no longer need this feature, you can block scripts by using the same steps, but in step 5, turn off the “Change execution policy to allow local PowerShell scripts […]” toggle switch.

From Windows 10

To change the PowerShell execution policy from the Settings app, use these steps:

  1. Open Settings.

  2. Click on Update & Security.

  3. Click the For developers page.

  4. Check the “Change execution policy to allow local PowerShell scripts […]” option for the PowerShell setting.

  5. Click the Apply button.

If you no longer need this feature, you can block scripts by using the same steps, but in step 4, clear the “Change execution policy to allow local PowerShell scripts […]” option and apply the changes.

After you complete the steps, the setting will set the execution policy for PowerShell to “RemoteSigned.” This policy will allow you to run scripts without signing but will require signing for remote scripts.

Change execution policy to allow script not digitally signed

If you have a PowerShell script that was created on another computer or downloaded from the internet, if you try to run the script, you might get the “File […].ps1 cannot be loaded. The file […].ps1 is not digitally signed. You cannot run this script on the current system” or the “The script will not execute on the system” error message. However, as long as you know the script is safe to use and you grabbed it from a trusted source, you can change the “Process” scope policy to run it successfully. 

PowerShell script not digitally signed error / Image: Mauro Huculak

To change the PowerShell execution policy to run a “.ps1” script that isn’t digitally signed, use these steps:

  1. Open Start.

  2. Search for PowerShell, right-click the top result, and select the Run as administrator option.

  3. Type the following command to change to allow PowerShell scripts with a digital signature and press Enter:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Once you complete the steps, you can run the script file using the .\Your-PowerShell-Script.ps1 command during the current session. After closing the PowerShell session, the execution policy will revert to its original stay to prevent other scripts from running. 

If you have to run another script file, you can repeat the steps outlined above.

Update October 10, 2024: This guide has been updated to ensure accuracy and reflect changes to the process.


Source link
Exit mobile version