Skip to content

Instantly share code, notes, and snippets.

@fbloemhof
Forked from Typiqally/install-theme-scheduler.ps1
Last active March 21, 2021 08:02
Show Gist options
  • Save fbloemhof/d10639d63e37ff08356362096e7b1647 to your computer and use it in GitHub Desktop.
Save fbloemhof/d10639d63e37ff08356362096e7b1647 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/FreekBloemhof/d10639d63e37ff08356362096e7b1647/raw/3dfa848af8a3d94bf9868f866d31d657e3f5e6c5/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 Get-UtcToDateTime($DateStr)
{
$Date = [DateTime]::Parse($DateStr)
[DateTime]::SpecifyKind($Date, [DateTimeKind]::Utc)
}
Function Get-UtcToLocalTime($UtcTime){
$TimeZone = Get-TimeZone
$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 = Get-UtcTolocalTime(Get-UtcToDateTime($Response.results.sunrise))
$SunSetDateTime = Get-UtcTolocalTime(Get-UtcToDateTime($Response.results.sunset))
if ($CurrentDateTime -gt $SunRiseDateTime -And $CurrentDateTime -lt $SunSetDateTime)
{
Enable-LightTheme(1)
} else {
Enable-LightTheme(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment