在登录、注销、启动、关闭时首先运行 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)

  1. Win+R
  2. 输入gpedit.msc并点击Enter按钮。
  3. 转到计算机配置(Computer Configuration)中的脚本(Scripts)
  4. 双击用户登录时首先运行 Windows PowerShell 脚本,注销(Run Windows PowerShell scripts first at user logon, logoff)
  5. 选择启用(Enabled)选项。
  6. 单击应用(Apply)确定(OK)
  7. 双击Run Windows PowerShell scripts first at computer startup, shutdown
  8. 选择启用(Enabled)选项。
  9. 单击应用(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 )选项。

在用户登录、注销、启动和关闭时首先运行 Windows PowerShell 脚本

单击 应用 (Apply )和 确定 (OK )以保存更改。

(Run Windows PowerShell)使用注册表编辑器(Registry Editor)在用户登录、注销、启动和关闭时首先运行 Windows PowerShell脚本

要使用注册表编辑器(Registry Editor)在用户登录、注销、启动和关闭时首先运行Windows PowerShell脚本,请执行以下步骤 -

  1. Win+R
  2. 键入regedit并点击Enter按钮。
  3. 单击(Yes)按钮。
  4. 转到HKLM中的系统( System)
  5. 右键单击(Right-click)System System > New > DWORD (32-bit) Value
  6. 将其命名为RunUserPSScriptsFirst
  7. 双击它并将(Value)数据设置为1
  8. 单击确定(OK)按钮。
  9. 右键单击(Right-click)System System > New > DWORD (32-bit) Value
  10. 将其命名为RunComputerPSScriptsFirst
  11. 双击它以将(Value)数据设置为1
  12. 单击确定(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

在用户登录、注销、启动和关闭时首先运行 Windows PowerShell 脚本

双击它并将 值数据 (Value data )设置为 1

在用户登录、注销、启动和关闭时首先运行 Windows PowerShell 脚本

按照相同的步骤创建另一个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 )”按钮进行确认。

希望能帮助到你。



About the author

我是一名 Windows 10 技术人员,多年来一直在帮助个人和企业利用新操作系统。我对 Microsoft Office 有丰富的知识,包括如何为不同的用户自定义外观和个性化应用程序。此外,我知道如何使用 Explorer 应用程序来探索和查找计算机上的文件和文件夹。



Related posts