powershell to set second monitor wallpaper

shark wsk 96 Reputation points
2021-11-08T13:15:44.48+00:00

I have Windows 10 laptop connected to two monitors in extended display mode. I would like to only set the second monitor wallpaper to a specific image.

Manual way of doing this would be to navigate: Desktop right click --> Personalization --> Choose the picture --> Right click and set for monitor 2

Is there any way to automate it via a script (bat/powershell) ?

I was able to get below code snippet to work, however it sets the wallpaper to both the displays

function Set-Wallpaper($MyWallpaper){
$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 
[Win32.Wallpaper]::SetWallpaper($MyWallpaper)
}

Set-WallPaper("C:\Wallpapers\nature.jpg")
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,379 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. shark wsk 96 Reputation points
    2021-11-10T22:45:49.177+00:00

    Any feedback..

    0 comments No comments