The default script execution policy settings in Windows 10 and 11 prevent unsigned PowerShell scripts from running. This means that if you try to manually run a PS1 script file or a PSM1 module from the powershell.exe (or pwsh.exe) console, you get an error:
File C:\PS\test_script.ps1 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.
On Windows, the ability to run PowerShell script files (with the *.PS1 extension) is restricted by the Script Execution Policy. List the current PowerShell script execution policy settings on the computer using the command:
Get-ExecutionPolicy –List
By default, the Restricted policy that is set for the LocalMachine scope prevents PowerShell scripts from running. The remaining four scopes (MachinePolicy, UserPolicy, Process, CurrentUser) are not configured (Undefined). This means that the LocalMachine scope policy applies to any PS1 script you try to run.
The default settings for the Script Execution Policy protect Windows from running potentially dangerous code in PowerShell scripts. In most cases, it is not recommended to change the script execution policy settings for security reasons.
If you only need to run a file that contains a PowerShell script once and you don’t want to change Windows security settings, it is better to temporarily allow to run scripts in the current PowerShell process.
Execute the command:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
Now try to run your PS1 file in the same console. The PowerShell script should run successfully.
As you can see, the Execution Policy value for the Process scope has changed to RemoteSigned. This option allows any local PS1 files to run, but it requires a trusted digital signature for any PowerShell files that are downloaded from the Internet. In this example, we changed the execution policy settings only for the current PowerShell process. After you close the pwsh.exe/powershell.exe console or end the Windows session, you cannot run PowerShell scripts until you change the execution policy again.
Set-ExecutionPolicy: Access to the path C:\Program Files\PowerShell\7\powershell.config.json is denied. To change the execution policy for the default (LocalMachine) scope, start PowerShell with the “Run as administrator” option. To change the execution policy for the current user, run Set-ExecutionPolicy -Scope CurrentUser.
If you want to run a PowerShell script from a BAT/CMD file or a Task Scheduler job, you can bypass the execution policy settings. To do this, run your PowerShell code in a separate process with the RemoteSigned or Bypass execution policy option.
Powershell.exe -noprofile -executionpolicy bypass -file c:\ps\test_script.ps1
This command allows you to run a PowerShell script and ignore the current execution policy settings.
If you want to allow the execution of any local PowerShell scripts, you can set the RemoteSigned policy for the LocalMachine scope (this is less secure!):
Set-ExecutionPolicy –ExecutionPolicy RemoteSigned
When you debug and develop PowerShell scripts in the Visual Studio Code environment, the error occurs: ps1 cannot be loaded because running scripts is disabled on this system. As a workaround, add the following settings to the settings.json file to always run the PowerShell script in bypass mode (ctrl + shift + p
-> type settings.json
):
“terminal.integrated.profiles.windows”: {
“PowerShell”: {
“source”: “PowerShell”,
“icon”: “terminal-powershell”,
“args”: [“-ExecutionPolicy”, “Bypass”]
}
},
“terminal.integrated.defaultProfile.windows”: “PowerShell”,
Restart the VSCode environment.