Skip to content

Instantly share code, notes, and snippets.

@Typiqally
Last active July 6, 2023 20:44
Show Gist options
  • Save Typiqally/3ee013f1576ba3066a3e20c5a8c3aa01 to your computer and use it in GitHub Desktop.
Save Typiqally/3ee013f1576ba3066a3e20c5a8c3aa01 to your computer and use it in GitHub Desktop.
Theme scheduler for Windows 10 Dark and Light mode based on sunset and sunrise time's, also includes a simple installer
New-Item -Path $env:LOCALAPPDATA -Name "Scripts" -ItemType "directory"
$url = "https://gist.githubusercontent.com/Typiqally/3ee013f1576ba3066a3e20c5a8c3aa01/raw/e21b2a7d205fdc2dc9c97a7de1dd91544d3e9569/theme-scheduler.ps1"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($url,"%LocalAppData%\Scripts\theme-scheduler.ps1")
$Path = $env:LOCALAPPDATA + "\Scripts\"
$output = [IO.Path]::Combine($Path, "theme-scheduler.ps1")
Write-Host "Downloading theme-scheduler from $url to path " $Path -ForegroundColor Green
#Download file
(New-Object System.Net.WebClient).DownloadFile($url, $output)
# Change these three variables to whatever you want
$jobname = "Theme Scheduler"
$script = $env:LOCALAPPDATA + "\Scripts\theme-scheduler.ps1"
$repeat = (New-TimeSpan -Minutes 5)
# The script below will run as the specified user (you will be prompted for credentials)
# and is set to be elevated to use the highest privileges.
# In addition, the task will run every 5 minutes or however long specified in $repeat.
$scriptblock = [scriptblock]::Create($script)
$trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeat
$msg = "Enter the username and password that will run the task";
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
Register-ScheduledJob -Name $jobname -ScriptBlock $scriptblock -Trigger $trigger -ScheduledJobOption $options -Credential $credential -RunNow
# Note: Make sure location services are enabled for apps, otherwise the script won't work.
Add-Type -AssemblyName System.Device
$GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher
$GeoWatcher.Start()
function Utc-ToDateTime($DateStr)
{
$Date = [DateTime]::Parse($DateStr)
[DateTime]::SpecifyKind($Date, [DateTimeKind]::Utc)
}
Function Utc-ToLocalTime($UtcTime){
$CurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($CurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UtcTime, $TimeZone)
Return $LocalTime
}
Function Enable-LightTheme($Enable) {
New-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $Enable -Type Dword -Force
}
while (($GeoWatcher.Status -ne 'Ready') -and ($GeoWatcher.Permission -ne 'Denied')) {
Start-Sleep -Milliseconds 100
}
if ($GeoWatcher.Permission -eq 'Denied'){
Write-Error 'Access Denied for Location Information, you need to allow apps to access your location.'
exit
}
$Location = $GeoWatcher.Position.Location
$Request = "https://api.sunrise-sunset.org/json?lat=" + $Location.Latitude + "&lng=" + $Location.Longitude
$Response = Invoke-WebRequest $Request | ConvertFrom-Json | Select results
$CurrentDateTime = Get-Date
$SunRiseDateTime = Utc-TolocalTime(Utc-ToDateTime($Response.results.sunrise))
$SunSetDateTime = Utc-TolocalTime(Utc-ToDateTime($Response.results.sunset))
if ($CurrentDateTime -gt $SunRiseDateTime -And $CurrentDateTime -lt $SunSetDateTime)
{
Enable-LightTheme(1)
} else {
Enable-LightTheme(0)
}
@mwnDK1402
Copy link

theme-scheduler.ps1 at line 34:
$Request = "https://api.sunrise-sunset.org/json?lat=" + $Location.Longitude + "&lng=" + $Location.Latitude
Latitude and longitude are swapped.

@Typiqally
Copy link
Author

Wow, cant believe I made that error. Must’ve been a hard day :) Thanks for pointing it out.

@mwnDK1402
Copy link

Wow, cant believe I made that error. Must’ve been a hard day :) Thanks for pointing it out.

Thanks for the script! I'm modifying it to change my background.

@100mph
Copy link

100mph commented Aug 28, 2021

Thank you for this script @Typiqally! Do you know how I can manually enter my region so it can follow the day/night cycle with that instead of giving my location? The less information I give Microsoft the better 😉

@Typiqally
Copy link
Author

Thank you for this script @Typiqally! Do you know how I can manually enter my region so it can follow the day/night cycle with that instead of giving my location? The less information I give Microsoft the better 😉

With the current API that is being used (https://sunrise-sunset.org/), I don’t think it’s possible.

You can also lookup the latitude and longitude of your region and use that. If you want to do this yourself, you do this by getting the latitude and longitude of the middle point of your region.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment