PoshCode Logo PowerShell Code Repository

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

Set-Wallpaper lets you set your windows desktop wallpaper, 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\Others Stock\Potter Wasp.jpg" "Stretched"
  6. ##    ls *.jpg | get-random | Set-Wallpaper
  7. ###################################################################################################
  8.  
  9. add-type @"
  10. using System;
  11. using System.Runtime.InteropServices;
  12. using Microsoft.Win32;
  13. namespace Wallpaper
  14. {
  15.   public enum Style : int
  16.   {
  17.       Tiled, Centered, Stretched
  18.   }
  19.  
  20.  
  21.   public class Setter {
  22.      public const int SetDesktopWallpaper = 20;
  23.      public const int UpdateIniFile = 0x01;
  24.      public const int SendWinIniChange = 0x02;
  25.  
  26.      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  27.      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  28.      
  29.      public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  30.         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  31.        
  32.         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  33.         switch( style )
  34.         {
  35.             case Style.Stretched :
  36.                 key.SetValue(@"WallpaperStyle", "2") ;
  37.                 key.SetValue(@"TileWallpaper", "0") ;
  38.                 break;
  39.             case Style.Centered :
  40.                 key.SetValue(@"WallpaperStyle", "1") ;
  41.                 key.SetValue(@"TileWallpaper", "0") ;
  42.                 break;
  43.             case Style.Tiled :
  44.                 key.SetValue(@"WallpaperStyle", "1") ;
  45.                 key.SetValue(@"TileWallpaper", "1") ;
  46.                 break;
  47.         }
  48.         key.Close();
  49.      }
  50.   }
  51. }
  52. "@
  53.  
  54. cmdlet Set-Wallpaper {
  55. Param(
  56.    [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
  57.    [Alias("FullName")]
  58.    [string]
  59.    $Path
  60. ,
  61.    [Parameter(Position=1, Mandatory=$true)]
  62.    [Wallpaper.Style]
  63.    $Style
  64. )
  65.    [Wallpaper.Setter]::SetWallpaper( (Convert-Path $Path), $Style )
  66. }

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