在登录、注销、启动、关闭时首先运行 Windows PowerShell 脚本
如果您想在用户登录、注销、启动和关闭时首先运行Windows PowerShell 脚本(Windows PowerShell scripts),您必须执行以下操作。您可以使用本地组策略编辑器(Local Group Policy Editor)和注册表编辑器将(Registry Editor)Windows PowerShell脚本优先于非 PowerShell 脚本。
当用户登录或启动计算机时,所有脚本同时运行。它可能会导致启动或运行特定程序时出现一些延迟。通常需要在非 PowerShell 脚本之前运行所有Windows PowerShell脚本和组策略对象。(Group Policy Object)
首先运行 Windows PowerShell(Run Windows PowerShell)脚本
要在用户登录、注销、启动和关闭时首先运行Windows PowerShell脚本,请执行以下步骤 -(Windows PowerShell)
- 按Win+R。
- 输入gpedit.msc并点击Enter按钮。
- 转到计算机配置(Computer Configuration)中的脚本(Scripts)。
- 双击用户登录时首先运行 Windows PowerShell 脚本,注销(Run Windows PowerShell scripts first at user logon, logoff)。
- 选择启用(Enabled)选项。
- 单击应用(Apply)和确定(OK)。
- 双击Run Windows PowerShell scripts first at computer startup, shutdown。
- 选择启用(Enabled)选项。
- 单击应用(Apply)和确定(OK)。
让我们详细检查这些步骤。
首先,您必须打开本地组策略编辑器(Local Group Policy Editor)。为此,请按 Win+R,键入gpedit.msc
,然后按 Enter 按钮。打开后,导航到以下路径-
Computer Configuration > Administrative Templates > System > Scripts
您将看到两个名为:
- 在用户登录、注销时首先运行 Windows PowerShell(Run Windows PowerShell)脚本,
- (Run Windows PowerShell)在计算机启动、关闭时首先运行 Windows PowerShell脚本。
双击(Double-click)它们中的每一个,然后选择 启用 (Enabled )选项。
单击 应用 (Apply )和 确定 (OK )以保存更改。
(Run Windows PowerShell)使用注册表编辑器(Registry Editor)在用户登录、注销、启动和关闭时首先运行 Windows PowerShell脚本
要使用注册表编辑器(Registry Editor)在用户登录、注销、启动和关闭时首先运行Windows PowerShell脚本,请执行以下步骤 -
- 按Win+R。
- 键入regedit并点击Enter按钮。
- 单击是(Yes)按钮。
- 转到HKLM中的系统( System)。
- 右键单击(Right-click)System System > New > DWORD (32-bit) Value。
- 将其命名为RunUserPSScriptsFirst。
- 双击它并将值(Value)数据设置为1。
- 单击确定(OK)按钮。
- 右键单击(Right-click)System System > New > DWORD (32-bit) Value。
- 将其命名为RunComputerPSScriptsFirst。
- 双击它以将值(Value)数据设置为1。
- 单击确定(OK)以保存更改。
要了解更多信息,请继续阅读。
在开始之前,建议 备份所有注册表文件(backup all Registry files) 并 创建一个系统还原点。
按 Win+R 打开运行提示符,键入regedit
,然后按 Enter 按钮。如果出现UAC提示,请单击 是 (Yes )按钮。之后,导航到此路径-
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
在这里,您必须创建两个REG_DWORD值。为此,右键单击System > New > DWORD (32-bit) Value 并将其命名为 RunUserPSScriptsFirst。
双击它并将 值数据 (Value data )设置为 1。
按照相同的步骤创建另一个DWORD(32 位)值(Value)并将其命名为 RunComputerPSScriptsFirst。然后,将 值数据 (Value data )设置为 1。
RunUserPSScriptsFirst表示 在用户登录、注销时首先运行 Windows PowerShell 脚本(Run Windows PowerShell scripts first at user logon, logoff) 设置,而RunComputerPSScriptsFirst定义 在计算机启动、关闭时首先运行 Windows PowerShell 脚本(Run Windows PowerShell scripts first at computer startup, shutdown) 设置。
如果您想恢复这些更改,您可以按照以下步骤操作。
如果您已使用本地组策略编辑器(Local Group Policy Editor)完成此操作,请打开相同的路径并选择 未配置 (Not Configured )选项。如果您已使用注册表编辑器(Registry Editor)完成此操作,请打开相同的 系统 (System )键并删除这两个REG_DWORD值。要删除它们,请右键单击它们中的每一个,选择“ 删除 (Delete )”选项,然后单击“ 确定 (OK )”按钮进行确认。
希望能帮助到你。
Related posts
Run PowerShell Scripts Locally无签名Windows 10
使用PowerShell Script禁用Windows 10中的Security Questions
如何使用Windows PowerShell ISE - 初级教程
如何在Windows 11/10查看PowerShell version
如何阻止IP或Windows 10使用PowerShell一个网站
Convert PowerShell script(PS1)文件至EXE,IExpress在Windows 10
如何在Windows 11中使用PowerShell创建计划任务
如何在Windows 10中使用PowerShell创建Local User Account
如何在Windows 10中使用PowerShell安装Appx应用程序包
在Windows 10上禁用CMD and PowerShell中的Scroll-Forward
Windows 10 Start Menu中Replace Powershell Command Prompt
Format External Hard Drive or USB驱动器使用Windows PowerShell
如何在Windows 11/10中卸载预安装的Microsoft Store Apps
如何在Windows 10中卸载PowerShell
Export and Backup Device Drivers在Windows 10中使用PowerShell
如何在Windows 10使用Public IP address PowerShell
PowerShell在Windows 10 Startup打开
如何在Windows 10中禁用PowerShell
通过PowerShell解决Windows Server Network connectivity问题问题
Reset Windows Update Client使用PowerShell Script