PoshCode Archive  Artifact [38ee129a50]

Artifact 38ee129a509e0c2eff2c43ac9e625cbf9aa78ce2ebf23792106e3ae1575faa3a:

  • File Set-Wallpaper-CTP3-fix.ps1 — part of check-in [272975fb4f] at 2018-06-10 13:37:44 on branch trunk — 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 … (user: Joel Bennett size: 4073)

# encoding: ascii
# api: csharp
# title: Set-Wallpaper (CTP3) fix
# description: 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 …
# version: 0.5
# type: script
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 4173
# x-archived: 2016-06-19T05:53:53
# x-published: 2013-05-20T11:56:00
#
# Updated for CTP3
# Made it run as a script instead of a function.
# command line fix 05 2013 
# # tested on win7 pro, execute cmd : powershell c:\path\script.ps1 “c:\path\wallpaper.jpg”
# See http://stackoverflow.com/questions/6147731/powershell-script-fails-first-time-but-works-second-time
#
#requires -version 2.0
## Set-Wallpaper - set your windows desktop wallpaper
###################################################################################################
## Usage:
##    Set-Wallpaper "C:\Users\Joel\Pictures\Wallpaper\Dual Monitor\mandolux-tiger.jpg" "Tile"
##    ls *.jpg | get-random | Set-Wallpaper
##    ls *.jpg | get-random | Set-Wallpaper -Style "Stretch"
###################################################################################################
## History:
##    v0.5  First release (on #PowerShell@irc.freenode.net)
##    v1.0  Public release (http://www.poshcode.org/488)
##          - Added Style: Tile|Center|Stretch
##    v1.1  (http://poshcode.org/491)
##          - Added "NoChange" style to just use the style setting already set
##          - Made the Style parameter to the cmdlet optional
##    v2.0  This Release
##          - Updated for CTP3, and made it run as a script instead of a function.
##    v2.0.1  Fix 05 2013
##          - Updated for command line execution in win7
##          - See : http://stackoverflow.com/questions/6147731/powershell-script-fails-first-time-but-works-second-time
###################################################################################################
[CmdletBinding()]
Param(
   [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
   [Alias("FullName")]
   [string]
   $Path
,
   [Parameter(Position=1, Mandatory=$false)]
   [Wallpaper.Style]
   $Style = "NoChange"
)

BEGIN {
try {
   $WP = [Wallpaper.Setter]
} catch {
   add-type -typedef @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
   public enum Style : int
   {
       Tile, Center, Stretch, NoChange
   }

   public class Setter {
      public const int SetDesktopWallpaper = 20;
      public const int UpdateIniFile = 0x01;
      public const int SendWinIniChange = 0x02;

      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
      
      public static void SetWallpaper ( string path, Wallpaper.Style style ) {
         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
         
         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
         switch( style )
         {
            case Style.Stretch :
               key.SetValue(@"WallpaperStyle", "2") ; 
               key.SetValue(@"TileWallpaper", "0") ;
               break;
            case Style.Center :
               key.SetValue(@"WallpaperStyle", "1") ; 
               key.SetValue(@"TileWallpaper", "0") ; 
               break;
            case Style.Tile :
               key.SetValue(@"WallpaperStyle", "1") ; 
               key.SetValue(@"TileWallpaper", "1") ;
               break;
            case Style.NoChange :
               break;
         }
         key.Close();
      }
   }
}
"@
   $WP = [Wallpaper.Setter]
}
}
PROCESS {
   Write-Verbose "Setting Wallpaper ($Style) to $(Convert-Path $Path)"
   $WP::SetWallpaper( (Convert-Path $Path), $Style )
}