PoshCode Logo PowerShell Code Repository

Set-Wallpaper (CTP3) by obaid 10 months ago (modification of post by Winfred view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2603"></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 …

*Fixed bug in $Style param.

  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.    $Style = "NoChange"
  28. )
  29.  
  30. BEGIN {
  31. try {
  32.    $WP = [Wallpaper.Setter]
  33. } catch {
  34.    $WP = add-type @"
  35. using System;
  36. using System.Runtime.InteropServices;
  37. using Microsoft.Win32;
  38. namespace Wallpaper
  39. {
  40.   public enum Style : int
  41.   {
  42.       Tile, Center, Stretch, NoChange
  43.   }
  44.  
  45.   public class Setter {
  46.      public const int SetDesktopWallpaper = 20;
  47.      public const int UpdateIniFile = 0x01;
  48.      public const int SendWinIniChange = 0x02;
  49.  
  50.      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  51.      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  52.      
  53.      public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  54.         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  55.        
  56.         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  57.         switch( style )
  58.         {
  59.            case Style.Stretch :
  60.               key.SetValue(@"WallpaperStyle", "2") ;
  61.               key.SetValue(@"TileWallpaper", "0") ;
  62.               break;
  63.            case Style.Center :
  64.               key.SetValue(@"WallpaperStyle", "1") ;
  65.               key.SetValue(@"TileWallpaper", "0") ;
  66.               break;
  67.            case Style.Tile :
  68.               key.SetValue(@"WallpaperStyle", "1") ;
  69.               key.SetValue(@"TileWallpaper", "1") ;
  70.               break;
  71.            case Style.NoChange :
  72.               break;
  73.         }
  74.         key.Close();
  75.      }
  76.   }
  77. }
  78. "@ -Passthru
  79. }
  80. }
  81. PROCESS {
  82.    Write-Verbose "Setting Wallpaper ($Style) to $(Convert-Path $Path)"
  83.    $WP::SetWallpaper( (Convert-Path $Path), $Style )
  84. }

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