Home > Common Problem > How to disable script execution in PowerShell on Windows 10/11?

How to disable script execution in PowerShell on Windows 10/11?

WBOY
Release: 2023-04-21 23:55:19
forward
3660 people have browsed it

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.

Fix 1 - Tweak PowerShell

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".

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

3. When the Windows PowerShell screen appears , type this code and change it accordingly, then press Enter to execute it.

powershell -ExecutionPolicy ByPass -脚本文件名.ps1
Copy after login

[

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
Copy after login

]

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

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.

Fix 2 – Change Execution Policy

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.

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

##3. When it opens,

Type this command to change the execution policy according to your choice.

Set-ExecutionPolicy RemoteSigned
Copy after login
Copy after login

[

You can set the script execution policy to four different modes. These are -

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
Copy after login
Copy after login

]

After that, press “

Y” and press Enter.

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

4. If you encounter any error message, just run this command.

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Copy after login

Again, write "

Y " and press Enter.

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

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 受限
Copy after login

3. Enter "

Y" again and press Enter key.

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

After that, close the PowerShell window.

Fix 3 – Edit the Registry

If 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.

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

警告 – 您将调整您的注册表。由于注册表编辑器是您系统中非常重要的一部分,您应该事先做好注册表备份。

当注册表编辑器打开时,点击“文件”。然后点击“导出”在您的系统上创建一个新的注册表备份。

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

稍后,如果出现问题,您可以使用此备份。

3. 现在,以这种方式展开左侧——

计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\ShellIds\ScriptedDiagnostics
Copy after login

4. 然后,双击“ ExecutionPolicy ”二进制值。

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

5. 只需在“数值数据:”框中写下“不受限制”。

6. 然后,点击“确定”保存此更改。

修复:在 Windows 11 ,10 上的 PowerShell 中在此系统上禁用运行脚本

之后,关闭注册表编辑器。关闭注册表编辑器后,重新启动系统。

重新启动计算机后,您可以轻松运行您选择的脚本。

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!

Related labels:
source:yundongfang.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template