Elevating Productivity: Exploring PowerShell Automation

Henry Ndou
4 min readFeb 23, 2024

--

When I embarked on my coding journey less than a year ago, I was enamored with dark mode. The sleek syntax highlighting and overall aesthetic appealed to me. However, my enthusiasm waned when I began to experience eye strain during extended coding sessions. I attributed this discomfort to the blue light emitted by my screen, which was partly true. Yet, my perspective shifted upon encountering research that delved deeper into the intricacies of visual perception.

This research illuminated how our eyes perceive objects optimally when they stand out distinctly from their surroundings in terms of color. For instance, it’s easier to spot a white object against a black background, and vice versa. However, this optimal perception is contingent upon certain conditions. Our eyes are naturally primed to function best during daylight, not because they are inherently sharper, but because of the abundance of ambient light. Conversely, in low-light conditions, such as nighttime, our pupils dilate to maximize light intake for better visibility. This evolutionary adaptation still influences our vision today.

Consider two key implications:

  1. During daylight, darker objects are easier to discern — hence, light mode facilitates visibility of darker objects.
  2. Conversely, in nighttime conditions, lighter objects are more visible — thus, dark mode enhances visibility of lighter objects.

Now, let us introduce screens into the equation. Imagine coding during the day when our eyes are adapted to perceive darker objects, yet using dark mode, which favors lighter objects. This discrepancy poses a challenge for our eyes: which implication should they adhere to? The eye must constantly toggle between these conflicting contexts, hindering optimal performance and potentially impacting productivity.

As a coder, I found myself burdened with the task of manually adjusting the screen theme based on the time of day. But what if we could automate this process? Enter PowerShell. Below, I present a script designed to accomplish this task, dissecting each line to elucidate its function.

Let us delve into the script and explore its capabilities.

$currentTime = Get-Date
$darkModeOnTime = Get-Date -Year $currentTime.Year -Month $currentTime.Month -Day $currentTime.Day -Hour 18 -Minute 0 -Second 0
$darkModeOffTime = Get-Date -Year $currentTime.Year -Month $currentTime.Month -Day $currentTime.Day -Hour 3 -Minute 0 -Second 0

if($currentTime -ge $darkModeOnTime -or $currentTime -lt $darkModeOffTime) {
Write-Host "Enabling Dark Mode"
New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "AppsUseLightTheme" -Value 0 -PropertyType DWord -Force
New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "SystemUsesLightTheme" -Value 0 -PropertyType DWord -Force
} else {
Write-Host "Disabling Dark Mode"
Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "AppsUseLightTheme" -ErrorAction SilentlyContinue
Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "SystemUsesLightTheme" -ErrorAction SilentlyContinue
}

Let us go through each part:

  1. $currentTime = Get-Date: This line retrieves the current date and time.
  2. $darkModeOnTime = Get-Date -Year $currentTime.Year -Month $currentTime.Month -Day $currentTime.Day -Hour 18 -Minute 0 -Second 0: This line sets the time when dark mode should be enabled. It sets the hour to 18 (6 PM), with minutes and seconds set to 0.
  3. $darkModeOffTime = Get-Date -Year $currentTime.Year -Month $currentTime.Month -Day $currentTime.Day -Hour 3 -Minute 0 -Second 0: This line sets the time when dark mode should be disabled. It sets the hour to 3 (3 AM), with minutes and seconds set to 0.
  4. if($currentTime -ge $darkModeOnTime -or $currentTime -lt $darkModeOffTime) {: This line checks if the current time is greater than or equal to the dark mode activation time or if it's before the dark mode deactivation time.
  5. Write-Host "Enabling Dark Mode": This line informs the user that dark mode is being enabled.
  6. New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "AppsUseLightTheme" -Value 0 -PropertyType DWord -Force: This line creates or updates the registry entry for applications to use the light theme, setting its value to 0 (indicating dark mode) under the specified registry path.
  7. New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "SystemUsesLightTheme" -Value 0 -PropertyType DWord -Force: This line creates or updates the registry entry for the system to use the light theme, setting its value to 0 (indicating dark mode) under the specified registry path.
  8. Write-Host "Disabling Dark Mode": This line informs the user that dark mode is being disabled.
  9. Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "AppsUseLightTheme" -ErrorAction SilentlyContinue: This line attempts to remove the registry entry for applications to use the light theme, suppressing errors if the entry does not exist.
  10. Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "SystemUsesLightTheme" -ErrorAction SilentlyContinue: This line attempts to remove the registry entry for the system to use the light theme, suppressing errors if the entry does not exist.

This script effectively adjusts the Windows theme settings to enable or disable dark mode based on the time of day, providing a more comfortable viewing experience for the user.

--

--

Henry Ndou
Henry Ndou

Written by Henry Ndou

Exploring all that which truly interests me

No responses yet