Skip to content

Powershell script which changes the active Windows theme and Desktop background based on daylight or a predefined schedule. Works in Windows 10/11. Not tested in Windows 7.

License

Notifications You must be signed in to change notification settings

unalignedcoder/auto-theme

Repository files navigation

AUTO THEME

Powershell script which changes the active Windows theme and Desktop background based on daylight or a predefined schedule. Works in Windows 10/11. Not tested in Windows 7.

Description

This script automatically switches the Windows active theme depending on Sunrise and Sunset, or hours set by the user.

Rather than relying on registry/system settings, it works by activating .theme files. This allows for a much higher degree of customization and compatibility.

It is designed to run in the background as a scheduled task, ensuring that the system theme is updated without user intervention.

It only connects to the internet to verify Location and retrieve Sunrise and Sunset times, via free api services such as sunrisesunset.io and ip-api.com.

Alternatively, it can stay completely offline operating on fixed hours provided by the user.

When run as the command .\AutoTheme.ps1 from terminal or desktop shortcut, the script toggles between themes, ignoring scheduled events.

 

Why, thank you for asking!
👉 You can donate to this project here👈

Installation

  1. Download the latest release and extract it to your preferred folder.

  2. Create custom Light and Dark themes. To do so, simply modify settings in the Personalize window (including colors or, for example, a wallpaper slideshow) and then save the theme.

    image
    In the Personalize>Themes windows, right click on a theme and select 'Save for sharing'.

  3. Open the file Config.ps1 and modify the following variables as preferred:

    image
    All entries in the config file come with exhaustive explanations.

  4. (optional) Run the script .\Setup.ps11 to create the main scheduled task. The script will ask for system privileges if not run as admin, and then proceed to create the "Auto Theme" task.

  5. (alternative) You can of course create the task yourself using Task Scheduler, setting the triggers to anything you prefer. In this case, make sure that the Action is set up as follows:

    • Program/script: Powershell.exe
    • Add arguments: -WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -NoProfile -File "C:\path\to\AutoTheme.ps1"
    • Run with highest privileges. It is advisable to always add the "On Workstation Unlock" trigger to the task. When the workstation is locked, the task may be unable to apply the theme fully, leaving out slideshow customizations and resulting in a hybrid "Custom" theme.
  6. When triggered, the task will then run the script AutoTheme.ps1. The script itself will schedule the next temporary task ("Sunrise Theme" or "Sunset theme") to run at the next required theme change time, whether set by the user or identified through user location.

Usage

This script is designed to run from Task Scheduler, and after the initial setup doesn't need interaction from the user.

When run from terminal, using .\AutoTheme.ps11, the script will 'toggle' the theme (switching from one .theme file to the other) and then exit, ignoring any scheduled event. This can be useful for testing purposes, but also for the odd times when there is need to manually switch the theme regardless of task settings.

GIF 13 03 2025 1-30-58
The command .\AutoTheme.ps1 can be run in terminal in verbose mode.

For convenience, a shortcut to the script can be created and placed on the desktop or taskbar for quick access. In this case, the shortcut should be to powershell.exe followed by the path to the script "C:\path\to\AutoTheme.ps1", indicating the same path in the Start in field:

image
A Windows shortcut can be created to directly toggle the theme.

Extra apps

Workarounds have been added for a couple of apps which do not toggle theme gracefully when the system theme changes: TrueLaunchBar and ProcessExplorer. More apps can be added if there is demand. More details in the Config file.

The forgotten benefits of using .theme files

Many scripts and apps try to automate dark and light theme functionality under Windows 10/11, but they do so by directly modifying system behavior, incurring in many difficulties and potential compatibility problems for the user.

This script however directly starts .theme files as processes (as if the user double-clicked on them), therefore letting Windows itself operate the entire visual transition, be it just the application of dark mode, or with addition of visual styles, wallpapers and more.

In fact, in addition to switching light and dark themes, using .theme files allows to set different wallpaper slideshows for each theme, while including other changes such as cursors, sounds, and more, all without forcing or tricking the system into unusual behavior.

All it takes are two .theme files (very easy to create, see Installation instructions above.)

Footnotes

  1. To run a PowerShell script on Windows, you need to set Execution Policy in PowerShell, using this command: Set-ExecutionPolicy RemoteSigned as Administrator. 2

About

Powershell script which changes the active Windows theme and Desktop background based on daylight or a predefined schedule. Works in Windows 10/11. Not tested in Windows 7.

Topics

Resources

License

Stars

Watchers

Forks