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.    public class Setter {
  20.       public const int SetDesktopWallpaper = 20;
  21.       public const int UpdateIniFile = 0x01;
  22.       public const int SendWinIniChange = 0x02;
  23.       [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  24.       private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  25.      
  26.       public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  27.          SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  28.          
  29.          RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  30.          switch( style )
  31.          {
  32.              case Style.Stretched :
  33.                  key.SetValue(@"WallpaperStyle", "2") ;
  34.                  key.SetValue(@"TileWallpaper", "0") ;
  35.                  break;
  36.              case Style.Centered :
  37.                  key.SetValue(@"WallpaperStyle", "1") ;
  38.                  key.SetValue(@"TileWallpaper", "0") ;
  39.                  break;
  40.              case Style.Tiled :
  41.                  key.SetValue(@"WallpaperStyle", "1") ;
  42.                  key.SetValue(@"TileWallpaper", "1") ;
  43.                  break;
  44.          }
  45.          key.Close();
  46.       }
  47.    }
  48. }
  49. "@
  50.  
  51. cmdlet Set-Wallpaper {
  52. Param(
  53.    [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
  54.    [Alias("FullName")]
  55.    [string]
  56.    $Path
  57. ,
  58.    [Parameter(Position=1, Mandatory=$true)]
  59.    [Wallpaper.Style]
  60.    $Style
  61. )
  62.    [Wallpaper.Setter]::SetWallpaper( (Convert-Path $Path), $Style )
  63. }

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