如何使用 Powershell 将注册表值获取到变量?(在 Windows 10 中实现以编程方式切换暗模式)

如何使用 Powershell 将注册表值获取到变量?(在 Windows 10 中实现以编程方式切换暗模式)

我正在为 Windows 10 实现暗黑模式切换器。我发现可以通过以下方式启用暗模式

Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value 0 -Type Dword -Force

和灯光模式

Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value 1 -Type Dword -Force

现在我想实现切换。我试过了

Get-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme

并没有得到 0 或 1,而是得到了如下输出:

AppsUseLightTheme : 1
PSPath            : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize
PSParentPath      : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes
PSChildName       : Personalize
PSDrive           : HKCU
PSProvider        : Microsoft.PowerShell.Core\Registry

我实际上如何将1其保存在变量中并进行切换?

答案1

好的,它实际上很简单,必须使用点符号来获取字段值:

$mode = (Get-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme).AppsUseLightTheme
$newMode = 1 - $mode
Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $newMode -Type Dword -Force

下面是一个 AutoHotKey 脚本,用于打开Win+暗模式Q

; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         Yakov Litvin
; Source:         https://superuser.com/a/1724237/576393
;
; Script Function:
;   toggle Windows dark mode on Win + Q
;
; based on https://stackoverflow.com/a/35844524/3995261

psScript =
(
  $mode = (Get-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme).AppsUseLightTheme
  $newMode = 1 - $mode
  Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $newMode -Type Dword -Force
)

#vk51::Run, PowerShell.exe -Command &{%psScript%},, hide

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

相关内容