A clean Windows 10 or 11 operating system with the default settings is blocking the execution of unsigned PowerShell scripts. This means that if you try to run the PS1 file manually from either powershell.exe or pwsh.exe, you will get an error message:
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 script files that contain PowerShell code (with a *.PS1 extension) is restricted by the Script Execution Policy settings.
Use the command to view the current settings of the PowerShell script execution policy on a computer:
Get-ExecutionPolicy –List
By default, the Restricted value in the LocalMachine scope prevents PowerShell script files from running on Windows. The remaining four scopes (MachinePolicy, UserPolicy, Process, CurrentUser) are not configured (Undefined). This means that the LocalMachine scope policy is applied by default.
The default settings for the Script Execution Policy help protect Windows from running potentially dangerous code in PowerShell scripts. In most cases, for security reasons, it is not recommended to change the default settings of the Scripting Execution Policy.
If you need to run a file of PowerShell scripts once and you don’t want to change Windows security settings, you can temporarily allow scripts to run in the current PowerShell process.
Run the command:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
Now try to run your PS1 file. The PowerShell script should successfully run in the console.
As you can see, the Execution Policy value for the Process scope has changed to RemoteSigned. This type of policy allows any local PS1 files to be launched. You can also run PS1 files downloaded from the Internet that have been digitally signed by a trusted publisher.
In this example, we have only changed the execution policy settings for the current PowerShell process. If you close the pwsh.exe/powershell.exe console or if you log off from Windows, you will not be able to run any PowerShell scripts. This method also allows you to override the current Execution Policy settings for non-admin users.
Note. If you try to change the Execution Policy settings for the LocalMachine or MachinePolicy scope under a non-admin user, an error occurs:
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 need to run a PowerShell script from a .bat file or a Task Scheduler job, you can bypass the Execution Policy settings.
To do this, you must run your PowerShell file in a separate process with the RemoteSigned or Bypass execution policy option:
powershell.exe -noprofile -executionpolicy bypass -file c:\ps\test_script.ps1
As you can see, this command allows you to run a PowerShell script and ignore the current execution policy security settings.
You can set the RemoteSigned policy for the LocalMachine scope if you always want to allow local PowerShell scripts to run on your computer (this is a less secure way!!!):
Set-ExecutionPolicy –ExecutionPolicy RemoteSigned
Now you can run any local PowerShell script files on Windows 10 or 11.
If you need to work around the “ps1 cannot be loaded because running scripts is disabled on this system” error in Visual Studio Code, add the following settings to your settings.json file (ctrl + shift + p -> type “settings.json”):
"terminal.integrated.profiles.windows": { "PowerShell": { "source": "PowerShell", "icon": "terminal-powershell", "args": ["-ExecutionPolicy", "Bypass"] } }, "terminal.integrated.defaultProfile.windows": "PowerShell",
Then restart VSCode.