Skip to content

Instantly share code, notes, and snippets.

@LaurentTreguier
Last active August 10, 2021 11:39
Show Gist options
  • Select an option

  • Save LaurentTreguier/effa119c41defbece38ec4a48647eea9 to your computer and use it in GitHub Desktop.

Select an option

Save LaurentTreguier/effa119c41defbece38ec4a48647eea9 to your computer and use it in GitHub Desktop.
Windows Night Switch

Windows Night Switch

Installing the script

The script can be put anywhere, for this gist it will be under C:\Program Files\Night Switch. Simply create the Night Switch directory, and place switch.ps1 inside it.

Script usage trivia

The script takes 2 arguments. The first one is the hour at which Windows should switch to light mode, and the second one when Windows should switch to dark mode. This script could probably take minutes into consideration, but I don't personally need this and I'm lazy.

Installing the task

  1. Launch the Tasks Scheduler app. Searching for "tasks" in the Windows search should be anough to find it.
  2. Create a new task by clicking on "Action", and "Create task".
  3. Put a name, such as "Night Switch", or any name you fancy.
  4. Check the radio button to execute the task even when the user isn't connected yet, and check the option to remember the password.
  5. Under the "triggers" tab, add two new triggers: check the "daily" radio button, and set the time to something like "08:00" and "22:00". The times should correspond to whichever time you want light mode and dark mode to be activated, respectively. Then, add two other triggers: upon opening and unlocking a session for a specific user (your user, which should be the same as the task author).
  6. Under the "actions" tab, add a new action to start a program. The path should be powershell.exe. Arguments should be -File "C:\Program Files\Night Switch\switch.ps1" 8 22, with whichever path points to switch.ps1 if placed under another drectory, and whichever hours you put in the triggers.
  7. Under "conditions" tab, uncheck the option to start the task only when the computer is plugged in, and its suboption to stop the task when the computer runs on battery.
  8. Under the "parameters" tab, check the option to start the task as soon as possible when a planned execution was missed.
  9. Finally, save the task, close the tasks scheduler, and enjoy Windows automatically switching between light and dark mode.
Start-Sleep 1
$CurrentTime = Get-Date
$MorningTime = (Get-Date -Hour $args[0] -Minute 0 -Second 0)
$EveningTime = (Get-Date -Hour $args[1] -Minute 0 -Second 0)
if (($CurrentTime -ge $MorningTime) -And ($CurrentTime -lt $EveningTime)) {
$LightTheme = 1
} else {
$LightTheme = 0
}
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize /v AppsUseLightTheme /t REG_DWORD /d $LightTheme /f
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize /v SystemUsesLightTheme /t REG_DWORD /d $LightTheme /f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment