Skip to content

Instantly share code, notes, and snippets.

@liflovs

liflovs/themeswitcher.ps1

Last active Mar 30, 2021
Embed
What would you like to do?
Powershell windows theme switch automation
function Delete-ScheduledTask([string] $taskName)
{
Write-Host "Delete schedule task for $taskName"
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
function DeleteIfExists-ScheduledTask([string] $taskName)
{
$exists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName}
if($exists)
{
Delete-ScheduledTask $taskName
}
}
[int]$lighttime = 7
[int]$nighttime = 19
$enable_light_apps = 'reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize /v AppsUseLightTheme /t REG_DWORD /d 1 /f'
$enable_light_system = 'reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize /v SystemUsesLightTheme /t REG_DWORD /d 1 /f'
$enable_dark_apps = 'reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize /v AppsUseLightTheme /t REG_DWORD /d 0 /f'
$enable_dark_system = 'reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize /v SystemUsesLightTheme /t REG_DWORD /d 0 /f'
[int]$hrs = Get-Date -Format HH
Write-host "Day boundaries: $lighttime - $nighttime. Current time $a "
if ($hrs -ge $lighttime -and $hrs -lt $nighttime) {
Write-Host "Its a day time!"
Invoke-Expression $enable_light_apps
Invoke-Expression $enable_light_system
} else {
Write-Host "Its a night time!"
Invoke-Expression $enable_dark_apps
Invoke-Expression $enable_dark_system
}
[string]$user_folder = $env:USERPROFILE
[string]$script_destination_file = "$user_folder\themeswitcher.ps1"
if ($PSScriptRoot -ne $user_folder) {
$MyInvocation.MyCommand.ScriptContents | Out-File $script_destination_file
}
$task_name = "Theme Switcher"
$user_name = [environment]::username
$trigger = @(
$(New-ScheduledTaskTrigger -At "$([string]$lighttime):00" -Daily),
$(New-ScheduledTaskTrigger -At "$([string]$nighttime):00" -Daily)
$(New-ScheduledTaskTrigger -AtLogOn -User $user_name)
)
DeleteIfExists-ScheduledTask($task_name)
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -File $script_destination_file"
Register-ScheduledTask -TaskName $task_name -Action $Action -Trigger $trigger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment