0

I wanted a program that changes my Wallpaper daily, but the "Daily Desktop Wallpaper"-App only can do Full-HD and the official program from Microsoft is not only Adware, but displays an ugly watermark in the bottom right corner and can't change the Lock Screen, so I made my own small script to do that (with some help) that I wanted to share so others don't have to waste their time with this (hence the long title). It uses an API from github.

To do this everyday automatically, put the following action in a Task Scheduler Task that starts daily at a specific time:
Program/script: powershell.exe
Add arguments: -executionPolicy bypass -WindowStyle hidden -File "path\to\changeDesktopToNewestInPicturesPath.ps1"

To the question:

I still have one small problem: How do I change the Lock Screen? The current implementation does not seem to work... (In comments at the end):
Also, any suggestions are very welcome, as I am still pretty new to Powershell.

$dir = "~/Pictures/DailyWallpapers"

if (-not (Test-Path -Path $dir)) {
  mkdir $dir
}

$bingApiRequest = Invoke-RestMethod -Uri "https://bing.biturl.top/?resolution=3840" -ContentType "application/json" -Method Get
$fileName = $bingApiRequest.url.split("=")[-1]
Invoke-WebRequest -Uri $bingApiRequest.url -OutFile "~/Pictures/DailyWallpapers/$($fileName)"


$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$filepath = $latest.FullName
$code = @'
using System.Runtime.InteropServices;

namespace Win32{
    
    public class Wallpaper{

      [DllImport("user32.dll", CharSet=CharSet.Auto)]
      static  extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;

      public static void SetWallpaper(string thePath){
         SystemParametersInfo(20,0,thePath,3);
      }
    }
}
'@
add-type $code

#Desktop Wallpaper
[Win32.Wallpaper]::SetWallpaper($filepath)


# $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
# if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
#   Write-Host "changing Lock Screen..."
#   #Lockscreen
#   $regKey = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP'
#   if (!(Test-Path -Path $regKey)) {
#     $null = New-Item -Path $regKey
#   }

#   Set-ItemProperty -Path $regKey -Name LockScreenImagePath -value $filepath 
#   Set-ItemProperty -Path $regKey -Name LockScreenImageUrl -value $filepath 
#   Set-ItemProperty -Path $regKey -Name LockScreenImageStatus -value 1
# }
MySurmise
  • 146
  • 11

0 Answers0