PowerShell is an excellent and advanced cross-platform task automation solution. Creating and running PowerShell scripts on Windows systems is generally very easy. However, you may be blocked by "Running scripts is disabled on this system". " If you run a script on your system, you will see an error message. If your system administrator has set a policy that prevents any unauthorized scripts from running on your system, you will see the specified error message. You must cancel Block policy or tweak the registry and you can run any script.
You can run code bypassing the current execution policy. So you don't have to tamper with it, but you You can run your script.
1. After pressing the Windows key and S key at the same time, enter "powershell".
2. Then, Right-click on "Windows PowerShell" and click "Run as administrator".
3. When the Windows PowerShell screen appears , type this code and change it accordingly, then press Enter to execute it.
powershell -ExecutionPolicy ByPass -脚本文件名.ps1
[
Just replace " nameofthescript .ps1” with the name of the script you are trying to run.
Example – Assuming the name of the script is “myscript”, the command would be –
powershell -ExecutionPolicy ByPass -File myscript.ps1
]
This way you can run any script without changing the current script execution policy. However, this won't work if you want to batch execute scripts or create automated scripts Effect.
You must look for the next fix for this.
You can change the current execution policy settings if you wish.
1. Press the Windows key again and enter " powershell".
2. Later, right-click " Windows PowerShell" and click "Run as administrator" to open a terminal with administrative rights.
##3. When it opens,Type this command to change the execution policy according to your choice.
Set-ExecutionPolicy RemoteSigned
Restricted – The default mode where PowerShell does not allow any scripts to be run on the system.
AllSigned – Only signed and authorized scripts are allowed in this mode Run.
RemoteSigned - In this mode, all scripts you download must be signed by a trusted signature.
Unrestricted - You can run any script you like. There are no boundaries to trusted signatures or trusted publishers.
Example – Suppose you want to set the policy setting to “RemoteSigned”, the command would be –Set-ExecutionPolicy RemoteSigned
Y” and press Enter.
4. If you encounter any error message, just run this command.Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Y " and press Enter.
Once you have done this, you can easily run the script as needed. Test by running the script.Note——
Script execution policies are often restricted because misconfigured or faulty scripts can cause fatal damage to the system. So once you've finished running the script, you can set the execution policy to "restricted" mode again. 1. Access PowerShell with administrative rights. 2. Now, just execute this single code to restrict any policy from running on the system.设置 ExecutionPolicy 受限
Y" again and press Enter key.
After that, close the PowerShell window. Fix 3 – Edit the RegistryIf you encounter any error messages while changing the execution policy, you can edit the Registry Editor. 1. Just enter "regedit.msc" in the search box of the taskbar.
2. After that, click on “Registry Editor” to access it.
警告 – 您将调整您的注册表。由于注册表编辑器是您系统中非常重要的一部分,您应该事先做好注册表备份。
当注册表编辑器打开时,点击“文件”。然后点击“导出”在您的系统上创建一个新的注册表备份。
稍后,如果出现问题,您可以使用此备份。
3. 现在,以这种方式展开左侧——
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\ShellIds\ScriptedDiagnostics
4. 然后,双击“ ExecutionPolicy ”二进制值。
5. 只需在“数值数据:”框中写下“不受限制”。
6. 然后,点击“确定”保存此更改。
之后,关闭注册表编辑器。关闭注册表编辑器后,重新启动系统。
重新启动计算机后,您可以轻松运行您选择的脚本。
The above is the detailed content of How to disable script execution in PowerShell on Windows 10/11?. For more information, please follow other related articles on the PHP Chinese website!