How to Fix “The execution of scripts is disabled on this system” in PowerShell

Symptoms

When you try to run a script in Microsoft Azure Active Directory Module for Windows PowerShell, you receive one of the following error messages:

File C:\my_script.ps1 cannot be loaded. The execution of scripts is disabled on this system. Please see “Get-Help about_signing” for more details.

File C:\Desktop\myscript.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.

File C:\my_script.ps1 cannot be loaded. The file C:\my_script.ps1 is not digitally signed. The script will not execute on the system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.

Cause

This issue may occur if one of the following factors is true:

  • The PowerShell version that you are using is higher than 5.1. The Azure Active Directory Module only works with PowerShell 3 to 5.1.
  • The execution policy is set to Restricted. Certain Windows PowerShell cmdlets can’t run if the policy is too restricted.

Resolution

To resolve this issue, follow these steps:

  1. Identify the PowerShell version by running $PSVersionTable.
  2. Run the Azure Active Directory Module for Windows PowerShell as an administrator. To do it, select Start, select All Programs, select Windows Azure Active Directory, right-click Windows Azure Active Directory Module for Windows PowerShell, and then select Run as administrator.
  3. Set the execution policy to Unrestricted. To do it, type the following cmdlet, and then press Enter:PowerShellCopySet-ExecutionPolicy Unrestricted
  4. Run the Windows PowerShell cmdlets that you want.
  5. Set the execution policy to Restricted. To do it, type the following cmdlet, and then press Enter:PowerShellCopySet-ExecutionPolicy Restricted

Ref: https://docs.microsoft.com/en-us/troubleshoot/azure/active-directory/cannot-run-scripts-powershell