PoshCode Logo PowerShell Code Repository

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

Set-Wallpaper lets you set your windows desktop wallpaper. It requires PInvoke and I wrote it using PowerShell 2’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  (http://poshcode.org/491)
  14. ##          - Added "NoChange" style to just use the style setting already set
  15. ##          - Made the Style parameter to the cmdlet optional
  16. ##    v2.0  This Release
  17. ##          - Updated for CTP3, and made it run as a script instead of a function.
  18. ###################################################################################################
  19. [CmdletBinding()]
  20. Param(
  21.    [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
  22.    [Alias("FullName")]
  23.    [string]
  24.    $Path
  25. ,
  26.    [Parameter(Position=1, Mandatory=$false)]
  27.    [Wallpaper.Style]
  28.    $Style = "NoChange"
  29. )
  30.  
  31. BEGIN {
  32. try {
  33.    $WP = [Wallpaper.Setter]
  34. } catch {
  35.    $WP = add-type @"
  36. using System;
  37. using System.Runtime.InteropServices;
  38. using Microsoft.Win32;
  39. namespace Wallpaper
  40. {
  41.   public enum Style : int
  42.   {
  43.       Tile, Center, Stretch, NoChange
  44.   }
  45.  
  46.   public class Setter {
  47.      public const int SetDesktopWallpaper = 20;
  48.      public const int UpdateIniFile = 0x01;
  49.      public const int SendWinIniChange = 0x02;
  50.  
  51.      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  52.      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  53.      
  54.      public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  55.         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  56.        
  57.         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  58.         switch( style )
  59.         {
  60.            case Style.Stretch :
  61.               key.SetValue(@"WallpaperStyle", "2") ;
  62.               key.SetValue(@"TileWallpaper", "0") ;
  63.               break;
  64.            case Style.Center :
  65.               key.SetValue(@"WallpaperStyle", "1") ;
  66.               key.SetValue(@"TileWallpaper", "0") ;
  67.               break;
  68.            case Style.Tile :
  69.               key.SetValue(@"WallpaperStyle", "1") ;
  70.               key.SetValue(@"TileWallpaper", "1") ;
  71.               break;
  72.            case Style.NoChange :
  73.               break;
  74.         }
  75.         key.Close();
  76.      }
  77.   }
  78. }
  79. "@ -Passthru
  80. }
  81. }
  82. PROCESS {
  83.    Write-Verbose "Setting Wallpaper ($Style) to $(Convert-Path $Path)"
  84.    $WP::SetWallpaper( (Convert-Path $Path), $Style )
  85. }

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