Skip to content

Instantly share code, notes, and snippets.

@elfakyn
Last active March 4, 2024 14:44
Show Gist options
  • Select an option

  • Save elfakyn/9e1b60d48c2ff95f21a34ff924ed87ad to your computer and use it in GitHub Desktop.

Select an option

Save elfakyn/9e1b60d48c2ff95f21a34ff924ed87ad to your computer and use it in GitHub Desktop.
xc wallpaper
powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\D\HomeFolders\Pictures\xc\scripts\xc-wall.ps1"
$morning = Get-Date '07:00'
$day = Get-Date '09:00'
$evening = Get-Date '19:00'
$night = Get-Date '21:00'
$now = Get-Date
$fixedWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall.jpg"
$desiredWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall.jpg"
$desiredLightMode = 1
Function Set-WallPaper($Image) {
<#
.SYNOPSIS
Applies a specified wallpaper to the current user's desktop
.PARAMETER Image
Provide the exact path to the image
.EXAMPLE
Set-WallPaper -Image "C:\Wallpaper\Default.jpg"
#>
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Params
{
[DllImport("User32.dll",CharSet=CharSet.Unicode)]
public static extern int SystemParametersInfo (Int32 uAction,
Int32 uParam,
String lpvParam,
Int32 fuWinIni);
}
"@
$SPI_SETDESKWALLPAPER = 0x0014
$UpdateIniFile = 0x01
$SendChangeEvent = 0x02
$fWinIni = $UpdateIniFile -bor $SendChangeEvent
$ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
}
Function Set-LightMode($LightMode) {
Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value $LightMode -Type Dword -Force
Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $LightMode -Type Dword -Force
}
if ($now.TimeOfDay -lt $morning.TimeOfDay) {
$desiredWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall-night.jpg"
$desiredLightMode = 0
} elseif ($now.TimeOfDay -lt $day.TimeOfDay) {
$desiredWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall-dawn.jpg"
$desiredLightMode = 1
} elseif ($now.TimeOfDay -lt $evening.TimeOfDay) {
$desiredWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall-day.jpg"
$desiredLightMode = 1
} elseif ($now.TimeOfDay -lt $night.TimeOfDay) {
$desiredWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall-evening.jpg"
$desiredLightMode = 0
} else {
$desiredWallpaper = "C:\D\HomeFolders\Pictures\xc\xc-wall-night.jpg"
$desiredLightMode = 0
}
if (-not (Test-Path -Path $fixedWallpaper -PathType Leaf) -or (Compare-Object -ReferenceObject $(Get-Content $fixedWallpaper) -DifferenceObject $(Get-Content $desiredWallpaper))) {
Set-LightMode $desiredLightMode
Copy-Item $desiredWallpaper -Destination $fixedWallpaper
Set-WallPaper $fixedWallpaper
}
exit
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\D\HomeFolders\Pictures\xc\scripts\xc-wall.bat" & Chr(34), 0
Set WinScriptHost = Nothing
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2022-01-23T21:33:27.6658073</Date>
<Author>SIDHADE\elf</Author>
<URI>\xc-wall</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2022-01-23T07:00:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
<CalendarTrigger>
<StartBoundary>2022-01-23T09:00:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
<CalendarTrigger>
<StartBoundary>2022-01-23T19:00:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
<CalendarTrigger>
<StartBoundary>2022-01-23T21:00:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionUnlock</StateChange>
<Delay>PT5S</Delay>
</SessionStateChangeTrigger>
<LogonTrigger>
<Enabled>true</Enabled>
<Delay>PT5S</Delay>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-3361931160-831065677-218748086-1002</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1M</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\D\HomeFolders\Pictures\xc\scripts\xc-wall.vbs</Command>
</Exec>
</Actions>
</Task>
@elfakyn
Copy link
Author

elfakyn commented Feb 10, 2022

How this works:

The XML file is a task scheduler task rule. It needs to run at the desired hours of the day, as well as on unlock and on login.

The task scheduler runs a VB script which runs a batch script which runs a powershell script. Because of windows BS jank, this is the only way to prevent a window from popping up when the thing runs.

The powershell script compares the current wallpaper with the desired wallpaper and applies it if it detects a change (as well as sets the corresponding dark or light theme value). This way it won't hang your computer for a couple seconds if the wallpaper is the same.

Additionally, the script replaces the wallpaper file in-place. This is to ensure that it updates all desktops in a multi-desktop environment (for windows 11), not just the current one.

  • xc-wall.jpg is the image file that will be set as a wallpaper. You don't need to have this already, it will be created by the script.
  • xc-wall-____.jpg are the images for the various times of day.

To make the script work:

  1. In regedit, under HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize, create the DWORD Values SystemUsesLightTheme and AppsUseLightTheme of value 0 (0 is dark and 1 is light).
  2. edit the paths in the scripts to correspond to the correct script file locations and image file locations.
  3. make the scripts executable
  4. make the scripts only editable by administrators (this is important! otherwise someone with local access can escalate privileges via these scripts)
  5. make sure the image file locations match the values in the powershell script (just the xc-wall-____.jpg ones; xc-wall.jpg is automatically created)
  6. install the task scheduler rule
  7. run the task
  8. enjoy

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