PoshCode Logo PowerShell Code Repository

Set-Wallpaper (CTP2) (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/491"></script>download | new post

Set-Wallpaper lets you set your windows desktop wallpaper. It requires PInvoke and I wrote it using CTP2’s Add-Type, although it could be done in v1 using the JIT code generation tricks Lee Holmes has mentioned in the past …

  1. #requires -version 2.0
  2. ## Set-Wallpaper - set your windows desktop wallpaper
  3. ###################################################################################################
  4. ## Usage:
  5. ##    Set-Wallpaper "C:\Users\Joel\Pictures\Wallpaper\Dual Monitor\mandolux-tiger.jpg" "Tile"
  6. ##    ls *.jpg | get-random | Set-Wallpaper
  7. ##    ls *.jpg | get-random | Set-Wallpaper -Style "Stretch"
  8. ###################################################################################################
  9. ## History:
  10. ##    v0.5  First release (on #PowerShell@irc.freenode.net)
  11. ##    v1.0  Public release (http://www.poshcode.org/488)
  12. ##          - Added Style: Tile|Center|Stretch
  13. ##    v1.1  This Release
  14. ##          - Added "NoChange" style to just use the style setting already set
  15. ##          - Made the Style parameter to the cmdlet optional
  16. ###################################################################################################
  17.  
  18. add-type @"
  19. using System;
  20. using System.Runtime.InteropServices;
  21. using Microsoft.Win32;
  22. namespace Wallpaper
  23. {
  24.    public enum Style : int
  25.    {
  26.        Tile, Center, Stretch, NoChange
  27.    }
  28.    public class Setter {
  29.       public const int SetDesktopWallpaper = 20;
  30.       public const int UpdateIniFile = 0x01;
  31.       public const int SendWinIniChange = 0x02;
  32.       [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  33.       private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  34.      
  35.       public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  36.          SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  37.          
  38.          RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  39.          switch( style )
  40.          {
  41.             case Style.Stretch :
  42.                key.SetValue(@"WallpaperStyle", "2") ;
  43.                key.SetValue(@"TileWallpaper", "0") ;
  44.                break;
  45.             case Style.Center :
  46.                key.SetValue(@"WallpaperStyle", "1") ;
  47.                key.SetValue(@"TileWallpaper", "0") ;
  48.                break;
  49.             case Style.Tile :
  50.                key.SetValue(@"WallpaperStyle", "1") ;
  51.                key.SetValue(@"TileWallpaper", "1") ;
  52.                break;
  53.             case Style.NoChange :
  54.                break;
  55.          }
  56.          key.Close();
  57.       }
  58.    }
  59. }
  60. "@
  61.  
  62. cmdlet Set-Wallpaper {
  63. Param(
  64.    [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
  65.    [Alias("FullName")]
  66.    [string]
  67.    $Path
  68. ,
  69.    [Parameter(Position=1, Mandatory=$false)]
  70.    [Wallpaper.Style]
  71.    $Style = "NoChange"
  72. )
  73.    [Wallpaper.Setter]::SetWallpaper( (Convert-Path $Path), $Style )
  74. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me