Created
February 19, 2018 16:06
-
-
Save PSingletary/f8b963a3f9e63621b640e5f42d834cd5 to your computer and use it in GitHub Desktop.
Download Images for Widnows 10 Wallpaper - https://www.reddit.com/r/Windows10/comments/7ybpcl/setbingwallpaperasdesktopps1/?st=JDSIKA3X&sh=f09b4c1a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Set-BingWallpaperAsDesktop.ps1 - [email protected] | |
| # Great help from: https://www.kittell.net/code/powershell-remove-windows-wallpaper-per-user/ | |
| #$DebugPreference = 'Continue'; #uncomment if you want to see all the steps | |
| #region types | |
| Add-Type @" | |
| using System; | |
| using System.Runtime.InteropServices; | |
| using Microsoft.Win32; | |
| namespace Wallpaper | |
| { | |
| public class Setter { | |
| public const int SetDesktopWallpaper = 20; | |
| public const int UpdateIniFile = 0x01; | |
| public const int SendWinIniChange = 0x02; | |
| [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
| private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); | |
| public static void SetWallpaper ( string path ) { | |
| SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange ); | |
| } | |
| } | |
| } | |
| "@ | |
| #endregion types | |
| #region functions | |
| Function Invoke-Update { | |
| $cmd = $env:windir + '\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters'; | |
| Invoke-Expression -Command $cmd; | |
| } | |
| Function Set-WallPaper | |
| { | |
| param ( | |
| [Parameter(Mandatory)] | |
| [ValidateScript({ Test-Path $_ })] | |
| [string]$WallPaper, | |
| [ValidateSet('Fill', 'Fit', 'Stretch', 'Center', 'Tile', 'Span')] | |
| [string]$WallpaperStyle = 'Fill', | |
| [switch]$AutoColorization | |
| ) | |
| #Affected Reg Keys | |
| #AutoColorization REG_DWORD 1 or 0 | |
| #WallpaperStyle REG_SZ 10, 6, 2, 0, 22 | |
| #Wallpaper REG_SZ path-to-image | |
| #remove cached files to help change happen | |
| #Remove-Item -Path "$($env:APPDATA)\Microsoft\Windows\Themes\CachedFiles" -Recurse -Force -ErrorAction SilentlyContinue | |
| $fit = @{ 'Fill' = 10; 'Fit' = 6; 'Stretch' = 2; 'Center' = 0; 'Tile' = '99'; 'Span' = '22' } | |
| if($AutoColorization) { | |
| Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name AutoColorization -value 1; | |
| } else { | |
| Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name AutoColorization -value 0; | |
| } | |
| if($WallpaperStyle -eq 'Tile') { | |
| Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -value 0; | |
| Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -value 1; | |
| } else { | |
| Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -value $fit[$WallpaperStyle]; | |
| Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -value 0; | |
| } | |
| [Wallpaper.Setter]::SetWallpaper($WallPaper); | |
| } | |
| #endregion functions | |
| $folder = Join-Path ([Environment]::GetFolderPath("MyDocuments")) 'BingDesktopImages'; | |
| if(-not(Test-Path $folder)) { $nul = New-Item $folder -ItemType Directory -ErrorAction Stop; } | |
| $folder = Join-Path $folder (Get-Date).ToString('yyyy-MM'); | |
| if(-not(Test-Path $folder)) { $nul = New-Item $folder -ItemType Directory -ErrorAction Stop; } | |
| $base = 'http://www.bing.com'; | |
| Write-Debug ('$base: ' + $base); | |
| $metaURI = $base + '/HPImageArchive.aspx?format=js&idx=0&n=10&mkt=en-US'; | |
| Write-Debug ('$metaURI: ' + $metaURI); | |
| $data = Invoke-WebRequest -Uri $metaURI | ConvertFrom-Json; | |
| $count = $data.images.Count; | |
| if($count -lt 1) { | |
| throw 'Failed to get Bing images data'; | |
| return; | |
| } | |
| $use = Get-Random -Maximum $count; | |
| $currentWP = (Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallPaper).WallPaper; | |
| for ($i=0;$i -lt $count;$i++) { | |
| $imageURI = $base + $data.images[$i].url; | |
| Write-Debug ('$imageURI: ' + $imageURI); | |
| $file = Split-Path $imageURI -Leaf; #filename for local use | |
| $savePath = Join-Path $folder $file; | |
| Write-Debug ('$savePath: ' + $savePath); | |
| if($i -eq $use -or $savePath -eq '') { | |
| if($forDesktop -ne $currentWP) { $forDesktop = $savePath; } #don't use current image | |
| } | |
| #don't download if it already exists | |
| if(Test-Path $savePath) { | |
| Write-Host ('Image exists: ' + $file); | |
| continue; | |
| } | |
| Write-Host ('Retreiving image: ' + $file); | |
| $result = Invoke-WebRequest -Uri $imageURI -OutFile $savePath; | |
| } | |
| Write-Host ('Setting wallpaper: ' + $forDesktop); | |
| Set-wallpaper -Wallpaper $forDesktop -WallpaperStyle Fill -AutoColorization; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment