PoshCode Archive  Artifact Content

Artifact 33ac35299edb02a714671e56920eb63b6f321baf448a0c8ca4c6f523bf99d5f1:

  • File Set-Wallpaper-CTP3.ps1 — part of check-in [f8948f3c89] at 2018-06-10 14:00:02 on branch trunk — Minor bugfix (user: dzmanto size: 3735)

# encoding: ascii
# api: csharp
# title: Set-Wallpaper (CTP3)
# description: Minor bugfix
# version: 0.5
# type: script
# author: dzmanto
# license: CC0
# x-poshcode-id: 5720
# x-archived: 2016-05-04T06:05:50
# x-published: 2016-01-30T13:31:00
#
# The SetWallpaper script is around in various places on the internet. The script is nice. Actually, the RegistryKey key = ... should be set prior to calling SystemParametersInfo. This fix allows changes in WallpaperStyle to take immediate effect.
# Added ValidateSet to make things easier
#
#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.
###################################################################################################
[CmdletBinding()]
Param(
   [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
   [Alias("FullName")]
   [string]
   $Path
,
   [Parameter(Position=1, Mandatory=$false)]
   [String][ValidateSet("Tile", "Center", "Stretch", "NoChange")]
   $Style = "NoChange"
)

BEGIN {
try {
   $WP = [Wallpaper.Setter]
} catch {
   $WP = add-type @"
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 ) {
        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();
         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
      }
   }
}
"@ -Passthru
}
}
PROCESS {
## you may consider $path_as_path = [IO.Path]::GetFullPath( $Path ) instead of $(Convert-Path $Path)
   Write-Verbose "Setting Wallpaper ($Style) to $(Convert-Path $Path)"
   $WP::SetWallpaper( (Convert-Path $Path), $Style )
}