Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save realslacker/81a07dac4d0c4cf379e05ca62420353e to your computer and use it in GitHub Desktop.
Save realslacker/81a07dac4d0c4cf379e05ca62420353e to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Update your desktop wallpaper from Windows Spotlight using PowerShell
.NOTES
Author: Shannon Graybrook
Github: github.com/realslacker
#>
#requires -version 5.1
using namespace System.Drawing
using namespace System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
# C# From: https://kelleymd.wordpress.com/2015/01/10/update-wallpaper-image
# Use C# code to call an immediate refresh on the screensaver image. The style
# is not changed here (strech, tile etc…) so whatever was set before will
# remain.
# SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)
Add-Type @'
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public class UpdateImage
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void Refresh(string path)
{
SystemParametersInfo( 20, 0, path, 0x01 | 0x02 );
}
}
}
'@
# check the current wallpaper
$CurrentWallpaper = Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name Wallpaper
# check display orientation from primary screen
$Orientation = [Screen]::PrimaryScreen |
ForEach-Object { ( 'Portrait', 'Landscape' )[ ( $_.Bounds.Width / $_.Bounds.Height ) -ge 1 ] }
# get Windows Spotlight wallpapers matching the screen orientation, then set
# the background image for the current user
Get-ChildItem -Path "$env:LOCALAPPDATA\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets" |
Where-Object { $_.Length -gt 100000 } |
Sort-Object -Descending CreationTime |
Where-Object { ( [Image]::FromFile( $_.FullName ) | %{ ( 'Portrait', 'Landscape' )[ ( $_.Width / $_.Height ) -ge 1 ] } ) -eq $Orientation } |
Select-Object -First 1 |
Where-Object { $_.FullName -ne $CurrentWallpaper } |
ForEach-Object { [Wallpaper.UpdateImage]::Refresh( $_.FullName ) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment