按一天中的时间自动切换 Windows 10 中的暗/亮模式(无需修改或更改主题!)

Seb*_*ste 4 powershell scheduler scheduled-tasks windows-10 darkmode

如今大多数设备都允许自动切换暗/亮模式,但 Windows 10 似乎没有这样的功能。有办法做到这一点吗?例如使用任务计划程序?

似乎有很多关于如何以编程方式更改窗口“主题”的示例,但没有关于亮/暗模式切换的示例(可以在“设置/颜色”中为“Windows 模式”或“应用程序模式”独立设置)。

Windows 10 设置颜色深色/浅色模式部分

Seb*_*ste 6

就在这里!

无缝地完成可能有点棘手,但可以通过以下步骤完成:

打开任务计划程序并使用以下设置创建一个新任务:

一般的

常规任务设置

  • 需要“无论用户是否登录都运行”,这样每次运行时 powershell 窗口都不会闪烁。

  • “不存储密码”检查,因为它是离线脚本

  • “以最高权限运行”只是为了确保没有任何事情打扰它(可选)

触发器

登录时触发任务设置 自午夜起每小时触发任务设置

  • 添加一个“在工作站解锁”触发器
  • 添加一个触发器以每小时重复一次

行动

运行PS脚本的动作

  • 使用 Program=Powershell 和 Arguments= 添加操作$time=(Get-Date).TimeOfDay.Hours; if($time -gt 8 -and $time -lt 19){"Setting Light theme..";Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value 1 -Type Dword -Force; Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value 1 -Type Dword -Force} else {"Setting Dark theme..."; 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 SystemUsesLightTheme -Value 0 -Type Dword -Force;}

该 PS 脚本看起来很多都打包在一行中,但这只是我们可以将其粘贴到操作中的“参数”对话框中。让我们把它分解一下看看它做了什么:

# Set current time in a variable
$time=(Get-Date).TimeOfDay.Hours; 
# if later than 8am and earlier than 7pm, use light mode
if($time -gt 8 -and $time -lt 19){ 
    "Setting Light theme.."; # output in case we let a window be opened
    # set "app" system mode to "light"
    Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value 1 -Type Dword -Force; 
    # set "OS" system mode to "light"
    Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value 1 -Type Dword -Force; 
} else {
    "Setting Dark theme..."; # output in case we let a window be opened
    # set "app" system mode to "dark"
    Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value 0 -Type Dword -Force; 
    # set "OS" system mode to "dark"
    Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value 0 -Type Dword -Force; 
}
Run Code Online (Sandbox Code Playgroud)

更新:

尽管非常紧凑,但在单行中维护代码可能会很麻烦,因此如果您希望将 PS 脚本保存在文件中.ps1,则只需将Powershell.exe(或pwsh.exe) 放入程序框中,然后将其-file C:\yourscript.ps1作为参数即可从那里执行它):

任务计划程序操作执行文件而不是在线命令