Powershell to set second monitor wallpaper

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")

I found a lovely module that has exactly what you need.

Install-Module -Name FP.SetWallpaper

# Show your monitors
Get-Monitor

# Assume you are wanting the second one listed, grab it and pipe to Set-WallPaper
Get-Monitor | Select-Object -Last 1 | Set-WallPaper -Path C:\Wallpapers\nature.jpg
3 Likes

Thank you for your feedback. When I tried to install the module, I get the below error

WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'

I then tried to update the PowerShellGet and get below error


PackageManagement\Install-Package : The following commands are already available on this                                system:'Find-Package,Install-Package,Uninstall-Package'. This module 'PackageManagement' may override the existing
commands. If you still want to install this module 'PackageManagement', use -AllowClobber parameter.
At C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package],
   Exception
    + FullyQualifiedErrorId : CommandAlreadyAvailable,Validate-ModuleCommandAlreadyAvailable,Microsoft

.PowerShell.Pack
   ageManagement.Cmdlets.InstallPackage

As the error states, you already have command Install-Package on your system so in order to install the new PackageManagement module, you need to decide if you want to overwrite the existing command. If so, use the -AllowClobber parameter as suggested.

Install-Module packagemanagement -AllowClobber
1 Like

Ok, I did try and update hte packagemanagement module and retried installing the Wallpaper module, still the same error

PS C:\Users\XXXXX> Install-Module packagemanagement -AllowClobber
PS C:\Users\XXXXX> Get-Module packagemanagement

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     1.0.0.1    PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package, Get-Pack...

Here is the verbose print of the FS.Wallpaper installation attempt

Install-Module -Name FP.SetWallpaper -Verbose
VERBOSE: Using the provider 'PowerShellGet' for searching packages.
VERBOSE: The -Repository parameter was not specified.  PowerShellGet will use all of the registered repositories.
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'.
VERBOSE: The specified Location is 'https://www.powershellgallery.com/api/v2' and PackageManagementProvider is 'NuGet'.
VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='FP.SetWallpaper'' for
''.
VERBOSE: Total package yield:'1' for the specified package 'FP.SetWallpaper'.
VERBOSE: Performing the operation "Install-Module" on target "Version '1.0.1' of module 'FP.SetWallpaper'".

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
VERBOSE: The installation scope is specified to be 'AllUsers'.
VERBOSE: The specified module will be installed in 'C:\Program Files\WindowsPowerShell\Modules'.
VERBOSE: The specified Location is 'NuGet' and PackageManagementProvider is 'NuGet'.
VERBOSE: Downloading module 'FP.SetWallpaper' with version '1.0.1' from the repository
'https://www.powershellgallery.com/api/v2'.
VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='FP.SetWallpaper'' for
''.
VERBOSE: InstallPackage' - name='FP.SetWallpaper',
version='1.0.1',destination='C:\Users\XXXXX\AppData\Local\Temp\1423297965'
VERBOSE: DownloadPackage' - name='FP.SetWallpaper',
version='1.0.1',destination='C:\Users\XXXXX\AppData\Local\Temp\1423297965\FP.SetWallpaper\FP.SetWallpaper.nupkg',
uri='https://www.powershellgallery.com/api/v2/package/FP.SetWallpaper/1.0.1'
VERBOSE: Downloading 'https://www.powershellgallery.com/api/v2/package/FP.SetWallpaper/1.0.1'.
VERBOSE: Completed downloading 'https://www.powershellgallery.com/api/v2/package/FP.SetWallpaper/1.0.1'.
VERBOSE: Completed downloading 'FP.SetWallpaper'.
VERBOSE: Hash for package 'FP.SetWallpaper' does not match hash provided from the server.
VERBOSE: InstallPackageLocal' - name='FP.SetWallpaper',
version='1.0.1',destination='C:\Users\XXXXX\AppData\Local\Temp\1423297965'
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.

That is an old version of packagemanagement, but you don’t show anything regarding PowerShellGet.

What version of powershell are you running anyways? You can check with

$PSVersionTable

I’d try running

Install-Module PowershellGet -AllowClobber

then try again with the wallpaper module.

Here is the session log

PS C:\Users\XXXXX> $PSVersionTable                                                                                                                                                                                                            Name                           Value
----                           -----
PSVersion                      5.1.18362.1801
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1801
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\XXXXX> Install-Module PowershellGet -AllowClobber
PS C:\Users\XXXXX> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.18362.1801
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1801
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\XXXXX>
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>

What version of PowershellGet are you showing now?

Get-Module PowerShellGet -ListAvailable

I show

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}

If it’s not 2.2.5 then you may have luck specifying the version

Install-Module powershellget -RequiredVersion 2.2.5

Here is my print

PS C:\Users\XXXXX> Get-Module PowerShellGet -ListAvailable


    Directory: C:\Program Files (x86)\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}

Try

Install-Module PowershellGet -scope currentuser 
PS C:\Users\XXXXX> Install-Module PowershellGet -scope currentuser
PS C:\Users\XXXXX>                                                                                                   
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Get-Module PowerShellGet -ListAvailable                                                                                                                                                                                                                                                                                                                Directory: C:\Program Files (x86)\WindowsPowerShell\Modules                                                                                                                                                                                                                                                                                                         ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper                                                               
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper -Scope CurrentUser                                            
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>
PS C:\Users\XXXXX> Install-Module PowershellGet -scope currentuser
PS C:\Users\XXXXX>                                                                                                   
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Get-Module PowerShellGet -ListAvailable                                                                                                                                                                                                                                                                                                                Directory: C:\Program Files (x86)\WindowsPowerShell\Modules                                                                                                                                                                                                                                                                                                         ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper                                                               
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper -Scope CurrentUser                                            
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>

Here is the logs

PS C:\Users\XXXXX> Install-Module PowershellGet -scope currentuser
PS C:\Users\XXXXX>                                                                                                   
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Get-Module PowerShellGet -ListAvailable                                                                                                                                                                                                                                                                                                                Directory: C:\Program Files (x86)\WindowsPowerShell\Modules                                                                                                                                                                                                                                                                                                         ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}


PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper                                                               
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX>                                                                                                    
PS C:\Users\XXXXX> Install-Module -Name FP.SetWallpaper -Scope CurrentUser                                            
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
WARNING: The specified module 'FP.SetWallpaper' with PowerShellGetFormatVersion '2.0' is not supported by the current
version of PowerShellGet. Get the latest version of the PowerShellGet module to install this module, 'FP.SetWallpaper'.
PS C:\Users\XXXXX>`Preformatted text`

Run this and see if you have a script/function in your documents folder.

Get-Module PowerShellGet -ListAvailable | ForEach-Object filelist

Logs

PS C:\Users\XXXXX> Get-Module PowerShellGet -ListAvailable | ForEach-Object filelist                                  C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1
C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Format.ps1xml
C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Resource.psd1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSGet.Format.ps1xml
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSGet.Resource.psd1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Format.ps1xml
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Resource.psd1

Here it is

PS C:\Users\earukan> Get-Module PowerShellGet -ListAvailable | ForEach-Object filelist                                  C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1
C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Format.ps1xml
C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Resource.psd1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSGet.Format.ps1xml
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSGet.Resource.psd1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Format.ps1xml
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Resource.psd1
PS C:\Users\earukan>

Delete the one in program files(x86) manually

@krzydoug - Thank you for persevering with me. I was able to install the module.

For reference, the below command was used to set the wallpaper to my desired monitor

Get-Monitor | Select-Object -skip 1 | Select-Object -First 1 | Set-WallPaper -Path C:\Wallpapers\nature.jpg

I have a followup question on displayswitch. I will check and raise a new thread

Thanks everyone for your feedback and comments, appreciated

This module is pretty limited… The monitor Ids that it returns doesn’t match any ID that already resides in WMI or the registry. It also doesn’t return any useful monitor information that would allow you to target certain monitors with a certain wallpaper. It would be nice to be able to match a screen resolution or aspect ratio to the monitor so that you could apply an appropriate wallpaper based on the resolution. That would be particularly useful in instances where one monitor is configured for landscape and the other for portrait.