Script Execution Policy

Set the script execution policy to control the script execution behavior.

In PowerShell, there are cases where scripts cannot be executed by default (Restricted) due to security reasons.
Therefore, if you want to execute the script, you need to change the execution policy.
You can also specify the scope when changing the execution policy.

Microsoft Docs
About Execution Policies
https://docs.microsoft.com/en-us/previous-versions/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.0

Example: Changing the script execution policy

An example where an error occurs in the Restricted state,
the execution policy of the current user is changed to RemoteSigned,
and the script is executed.

PS C:\Users\user\Desktop> Get-ExecutionPolicy
Restricted
PS C:\Users\user\Desktop> .\script.ps1
# Error occurs
 
PS C:\Users\user\Desktop> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
 
Execution Policy Change
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?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
PS C:\Users\user\Desktop> Get-ExecutionPolicy
RemoteSigned
PS C:\Users\user\Desktop> .\script.ps1
Hello!

Temporarily change the execution policy

When executing PowerShell, you can temporarily change the execution policy by adding -ExecutionPolicy.

cmd> powershell script.ps1
# Error occurs
 
cmd> powershell -ExecutionPolicy RemoteSigned .\script.ps1
Hello!
 
cmd> powershell .\script.ps1
# Error occurs

YouTube

Last modified October 11, 2022: update (8216439)