Marcio Cunha

How to automate dark mode in Windows using PowerShell

Learn how to create a PowerShell script to automatically switch between Windows light and dark themes based on the time of day.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • PowerShell enables reading Windows Registry keys where the system stores visual theme preferences.
  • The operating system's task scheduler runs the automation script independently without manual intervention.
  • Automatic theme transitions reduce eye strain in low-light environments during nighttime hours.
  • Automation scripts eliminate repetitive manual adjustments to visual settings throughout daily routines.
  • Using native operating system commands removes the need to install third-party background software.

Why automate theme switching in Windows

Working for long hours in front of a high-brightness monitor, especially in dark environments, causes eye fatigue and visual discomfort. Although Windows offers native support for dark mode, the system does not automatically switch themes based on the time of day by default, requiring manual adjustments in settings. To overcome this limitation, we can rely on native automation tools, much like we do when we need to clean temporary files in Windows using PowerShell to optimize computer performance.

In practice, this means writing a small script that directly modifies the registry keys responsible for the visual interface and combining it with the operating system's Task Scheduler. This approach eliminates the need to install unknown third-party utilities that consume background resources. Furthermore, having full control over the code ensures the process is secure, lightweight, and executed precisely at the times desired by the user.

Understanding where Windows stores theme preferences

Windows stores color preferences for apps and the operating system in the Registry, which acts as a large central database for internal system settings. When we change the theme in graphical settings, the system modifies specific numeric values within these registry keys. Knowing these paths is the first step toward creating any automated visual personalization routine.

The main keys controlling dark mode for applications and the system are located within the current user path. In practice, when we set the value to zero for these keys, Windows enables light mode; when we set the value to one, dark mode activates immediately. This same logic of directly manipulating system parameters is applied across various administrative tasks, such as when we learn how to clean the Windows registry with total safety to keep the operating system agile and free of orphaned entries.

Creating the PowerShell script to toggle dark mode

PowerShell is a task automation framework developed by Microsoft that combines a powerful command line with a robust scripting language. Let's create a simple script that modifies the visual state, allowing us to create two separate files: one for light mode and another for dark mode, or an intelligent script that toggles based on the system clock.

Below is an example of functional code that simultaneously changes app and OS themes to dark mode. To ensure everything works properly, copy the code block below and save it to a file with the .ps1 extension on your computer.

# Define registry path for system and app themes
$registryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize'

# Change values to enable dark mode (1 for dark, 0 for light)
Set-ItemProperty -Path $registryPath -Name 'AppsUseLightTheme' -Value 0
Set-ItemProperty -Path $registryPath -Name 'SystemUsesLightTheme' -Value 0

Write-Output 'Dark mode enabled successfully!'

To reverse the process and create the corresponding light mode file, simply change the final value from zero to one in both script properties. It is recommended to save both files in a dedicated script folder within your user profile to facilitate organization and subsequent integration with the system's task scheduler.

Automating execution with the Task Scheduler

Creating the script is only half the battle; true automation happens when we tell Windows to run it automatically at the appropriate times. For this, we use the Task Scheduler, a native tool that allows triggering programs and scripts based on time events, logins, or operating system startup.

The configuration process involves following a logical sequence of steps to ensure the task runs with correct permissions and without displaying annoying windows on the user's screen. The list below details the necessary actions to set the script running in a fully autonomous way:

  1. Open the Start menu, type Task Scheduler, and open the corresponding application.
  2. In the right-hand panel, click Create Basic Task and give it a descriptive name, such as Toggle Night Mode.
  3. Set the task trigger by choosing the Daily option and specifying the desired activation time.
  4. In the action step, select Start a program and enter the PowerShell executable path in the program field.
  5. In the arguments field, add the full path of your script wrapped in quotes to ensure correct reading and complete the wizard.

With this structure configured, Windows will switch visual themes without requiring any manual clicks. If you want a light theme during the day and a dark theme at night, simply create two distinct tasks in the scheduler: one pointing to the light mode activation script in the morning and another pointing to the nighttime script. To explore these architectural concepts further in production, see our related technical analysis on How to test an NTC thermistor with a bench multimeter.

Final thoughts on visual automation

Automating small daily tasks within the operating system is an excellent starting point for anyone looking to gain productivity and personalize their work environment. Using PowerShell alongside the Task Scheduler proves that we do not need to rely on heavy background software to obtain practical and functional features in Windows.

Beyond protecting eyesight against light spikes in dark environments, mastering these concepts paves the way for creating more advanced system maintenance and tuning routines. With scripts configured and tested, your computer will organically adapt to your daily workflow rhythm.