Advertisement
JorgTheElder

Set-BingWallpaperAsDesktop.ps1

Feb 17th, 2018
5,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Set-BingWallpaperAsDesktop.ps1 - JorgTheElder@Outlook.com
  2. # Great help from: https://www.kittell.net/code/powershell-remove-windows-wallpaper-per-user/
  3.  
  4. #$DebugPreference = 'Continue'; #uncomment if you want to see all the steps
  5.  
  6. #region types
  7.  
  8. Add-Type @"
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using Microsoft.Win32;
  12. namespace Wallpaper
  13. {
  14.   public class Setter {
  15.  
  16.      public const int SetDesktopWallpaper = 20;
  17.      public const int UpdateIniFile = 0x01;
  18.      public const int SendWinIniChange = 0x02;
  19.  
  20.      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  21.      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  22.  
  23.      public static void SetWallpaper ( string path ) {
  24.         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  25.      }
  26.   }
  27. }
  28. "@
  29.  
  30. #endregion types
  31.  
  32.  
  33. #region functions
  34.  
  35. Function Invoke-Update {
  36.  
  37.   $cmd = $env:windir + '\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters';
  38.   Invoke-Expression -Command $cmd;
  39.  
  40. }
  41.  
  42. Function Set-WallPaper
  43. {
  44.   param (
  45.  
  46.     [Parameter(Mandatory)]
  47.     [ValidateScript({ Test-Path $_ })]
  48.     [string]$WallPaper,
  49.  
  50.     [ValidateSet('Fill', 'Fit', 'Stretch', 'Center', 'Tile', 'Span')]
  51.     [string]$WallpaperStyle = 'Fill',
  52.  
  53.     [switch]$AutoColorization
  54.  
  55.   )
  56.  
  57.   #Affected Reg Keys
  58.   #AutoColorization REG_DWORD 1 or 0
  59.   #WallpaperStyle REG_SZ 10, 6, 2, 0, 22
  60.   #Wallpaper REG_SZ path-to-image
  61.  
  62.   #remove cached files to help change happen
  63.   #Remove-Item -Path "$($env:APPDATA)\Microsoft\Windows\Themes\CachedFiles" -Recurse -Force -ErrorAction SilentlyContinue    
  64.  
  65.   $fit = @{ 'Fill' = 10; 'Fit' = 6; 'Stretch' = 2; 'Center' = 0; 'Tile' = '99'; 'Span' = '22' }
  66.  
  67.   if($AutoColorization) {
  68.  
  69.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name AutoColorization -value 1;
  70.  
  71.   } else {
  72.  
  73.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name AutoColorization -value 0;
  74.  
  75.   }
  76.  
  77.   if($WallpaperStyle -eq 'Tile') {
  78.  
  79.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -value 0;
  80.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -value 1;
  81.  
  82.   } else {
  83.  
  84.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -value $fit[$WallpaperStyle];
  85.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -value 0;
  86.  
  87.   }
  88.  
  89.   [Wallpaper.Setter]::SetWallpaper($WallPaper);
  90.  
  91. }
  92. #endregion functions
  93.  
  94. $folder = Join-Path ([Environment]::GetFolderPath("MyDocuments")) 'BingDesktopImages';
  95.  
  96. if(-not(Test-Path $folder)) { $nul = New-Item $folder -ItemType Directory -ErrorAction Stop; }
  97.  
  98. $folder = Join-Path $folder (Get-Date).ToString('yyyy-MM');
  99.  
  100. if(-not(Test-Path $folder)) { $nul = New-Item $folder -ItemType Directory -ErrorAction Stop; }
  101.  
  102. $base = 'http://www.bing.com';
  103. Write-Debug ('$base: ' + $base);
  104.  
  105. $metaURI = $base + '/HPImageArchive.aspx?format=js&idx=0&n=10&mkt=en-US';
  106. Write-Debug ('$metaURI: ' + $metaURI);
  107.  
  108. $data = Invoke-WebRequest -Uri $metaURI -UseBasicParsing | ConvertFrom-Json;
  109.  
  110. $count = $data.images.Count;
  111.  
  112. if($count -lt 1) {
  113.   throw 'Failed to get Bing images data';
  114.   return;
  115. }
  116.  
  117. $use = Get-Random -Maximum $count;
  118.  
  119. $currentWP = (Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallPaper).WallPaper;
  120.  
  121. for ($i=0;$i -lt $count;$i++) {
  122.  
  123.   $imageURI = $base + $data.images[$i].url;
  124.   Write-Debug ('$imageURI: ' + $imageURI);
  125.  
  126.   $file = Split-Path $imageURI -Leaf; #filename for local use
  127.  
  128.   $savePath = Join-Path $folder $file;
  129.   Write-Debug ('$savePath: ' + $savePath);
  130.  
  131.   if($i -eq $use -or $savePath -eq '') {
  132.    if($forDesktop -ne $currentWP) { $forDesktop = $savePath; } #don't use current image
  133.   }
  134.  
  135.   #don't download if it already exists
  136.   if(Test-Path $savePath) {
  137.     Write-Host ('Image exists: ' + $file);
  138.     continue;
  139.   }
  140.  
  141.   Write-Host ('Retreiving image: ' + $file);
  142.   $result = Invoke-WebRequest -Uri $imageURI -OutFile $savePath -UseBasicParsing;
  143.  
  144. }
  145.  
  146. Write-Host ('Setting wallpaper: ' + $forDesktop);
  147.  
  148. Set-wallpaper -Wallpaper $forDesktop -WallpaperStyle Fill -AutoColorization;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement