Nowadays most devices allow for an automatic toggle of Dark/Light mode, however Windows 10 doesn't seem to have such a feature. Is there a way to accomplish this? Using the Task Scheduler for example?
There seem to be many examples on how to change the windows "theme" programmatically, but not the light/dark mode toggle (which can be independently set for "windows mode" or for "app mode" in Settings/Colors).
Yes, there is!
It can be a bit tricky to pull off seamesly, but it can be done with the following steps:
Open Task Scheduler and create a new task with the following settings:
General
"Run whether user is logged in or not" is required so that a powershell window doesn't flash every time this runs.
"Do not Store Password" check since it's an offline script
"Run with highest priviledges" just to be sure nothing interrupts it (optional)
Triggers
Actions
$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;}
That PS script seems like a lot packed into a single line, but that's just so we can paste it in that "arguments" diaglog in the Action. So let's break it down a bit to see what it does:
# 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;
}
Update:
Although neatly-compact, maintaining code in a single line can be a hassle, so if you prefer to keep your PS script in a .ps1
file, you can excecute it from there simply by putting Powershell.exe
(or pwsh.exe
) in the Program box, and then -file C:\yourscript.ps1
as argument):