Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Set-RedditWallpapersAsDesktop.ps1 - JorgTheElder@Outlook.com
- # 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")) 'RedditWallPapers';
- 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 = 'https://www.reddit.com/r/wallpapers/new/.rss?sort=top&t=day';
- Write-Debug ('$base: ' + $base);
- $data = Invoke-WebRequest -Uri $base;
- $xml = [xml]$data.Content;
- $links = $xml.feed.entry.link.href;
- $images = @();
- $links | ForEach-Object {
- $commentXML = [xml](Invoke-WebRequest ($_ + '.rss')).Content;
- if($commentXML.feed.entry[0].content.InnerText -match '"([^"]+\.(jpg|png))">\[link\]') {
- $images += $matches[1];
- }
- }
- $count = $images.Count;
- if($count -lt 1) {
- throw 'Failed to get reddit images';
- return;
- }
- $use = Get-Random -Maximum $count;
- $forDesktop = '';
- $currentWP = (Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallPaper).WallPaper;
- for ($i=0;$i -lt $count;$i++) {
- $imageURI = $images[$i]
- 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;
Add Comment
Please, Sign In to add comment