Skip to content

Instantly share code, notes, and snippets.

@mmotti
Last active September 18, 2024 13:02
Show Gist options
  • Save mmotti/f9c59aee78e390862d1927f13a096ef2 to your computer and use it in GitHub Desktop.
Save mmotti/f9c59aee78e390862d1927f13a096ef2 to your computer and use it in GitHub Desktop.
Set Windows 11 to use Dark Mode. Particularly useful as part of a logon script for Windows Sandbox.
$strRegJson = @"
[
{
"RegPath": "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes",
"Name": "CurrentTheme",
"Type": "STRING",
"Value": "C:\\Windows\\resources\\Themes\\dark.theme"
},
{
"RegPath": "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\HighContrast",
"Name": "Pre-High Contrast Scheme",
"Type": "STRING",
"Value": "C:\\Windows\\resources\\Themes\\dark.theme"
},
{
"RegPath": "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
"Name": "AppsUseLightTheme",
"Type": "DWORD",
"Value": 0
},
{
"RegPath": "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
"Name": "SystemUsesLightTheme",
"Type": "DWORD",
"Value": 0
},
{
"RegPath": "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes",
"Name": "ThemeMRU",
"Type": "STRING",
"Value": "C:\\Windows\\resources\\Themes\\dark.theme;C:\\Windows\\resources\\Themes\\aero.theme;"
},
{
"RegPath": "HKEY_CURRENT_USER\\Control Panel\\Desktop",
"Name": "WallPaper",
"Type": "STRING",
"Value": "C:\\Windows\\web\\wallpaper\\Windows\\img19.jpg"
}
]
"@
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SystemParametersInfo(uint action, uint param, IntPtr vparam, uint init);
}
"@
$objRegJson = $strRegJson | ConvertFrom-Json
if ($objRegJson) {
taskkill /f /im explorer.exe 2>&1> $null
$objRegJson | ForEach-Object {
if ($null -notin ($_.RegPath, $_.Name, $_.Type, $_.Value)) {
$regPath = switch -Regex ($_.RegPath) {
'^(HKEY_CURRENT_USER)' {$_ -replace $Matches[1], 'HKCU:'}
#'^(HKEY_LOCAL_MACHINE)' {$_ -replace $Matches[1], 'HKLM:'}
Default {continue}
}
try {
Set-ItemProperty -Path $regPath -Name $_.Name -Type $_.Type -Value $_.Value
}
catch {
throw 'An error occured whilst adding items to the registry.'
}
}
}
$SPI_SETDESKWALLPAPER = 0x0014
$SPIF_UPDATEINIFILE = 0x01
$SPIF_SENDCHANGE = 0x02
[User32]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, [IntPtr]::Zero, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE) | Out-Null
Start-Process explorer.exe
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment