PoshCode Logo PowerShell Code Repository

Set-Wallpaper (CTP2) (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | 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.  
  29.  
  30.   public class Setter {
  31.      public const int SetDesktopWallpaper = 20;
  32.      public const int UpdateIniFile = 0x01;
  33.      public const int SendWinIniChange = 0x02;
  34.  
  35.      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  36.      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  37.      
  38.      public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  39.         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  40.        
  41.         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  42.         switch( style )
  43.         {
  44.            case Style.Stretch :
  45.               key.SetValue(@"WallpaperStyle", "2") ;
  46.               key.SetValue(@"TileWallpaper", "0") ;
  47.               break;
  48.            case Style.Center :
  49.               key.SetValue(@"WallpaperStyle", "1") ;
  50.               key.SetValue(@"TileWallpaper", "0") ;
  51.               break;
  52.            case Style.Tile :
  53.               key.SetValue(@"WallpaperStyle", "1") ;
  54.               key.SetValue(@"TileWallpaper", "1") ;
  55.               break;
  56.            case Style.NoChange :
  57.               break;
  58.         }
  59.         key.Close();
  60.      }
  61.   }
  62. }
  63. "@
  64.  
  65. cmdlet Set-Wallpaper {
  66. Param(
  67.    [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
  68.    [Alias("FullName")]
  69.    [string]
  70.    $Path
  71. ,
  72.    [Parameter(Position=1, Mandatory=$false)]
  73.    [Wallpaper.Style]
  74.    $Style = "NoChange"
  75. )
  76.    [Wallpaper.Setter]::SetWallpaper( (Convert-Path $Path), $Style )
  77. }

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