Change window border, desktop icon spacing, desktop background color and wallpaper

Introduction

This sample demonstrates how to change the window border, desktop icon spacing, desktop background color, and wallpaper by using PowerShell script. This script completes the tasks by using P/Invoke to call Windows API functions.

Scenarios

IT administrators can use this script to adjust desktop appearance for a group of computers to keep a consistent user experience.

Script

This script contains the following advanced functions:
You can use this script in following ways:
Method 1:
1. Download the script and copy it to your computer.
2. Open the script file with Notepad or any other script editors.
3. Scroll down to the end of the script file, and then add the code to call the functions.
4. Save the file and then run the script on the computer.
Method 2:
1. Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
2. Run Import-Module cmdlet to import this module file in Exchange Management Shell.
Import-Module filepath\scriptname.psm1
To get the detailed information about how to use the functions, run the following command to retrieve the help information:
PS> Get-Help functionName -detailed
For example:
PS> Get-Help Set-OSCDesktopView -detailed

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
public static void SetWallpaper(string path, WallpaperStyle style) 
        { 
            if (path == string.Empty ) 
            { 
                RemoveWallpaper(); 
            } 
            elseif (System.IO.File.Exists(path) &&  
                ( 
                    path.Trim().ToLowerInvariant().EndsWith(".bmp")|| 
                    path.Trim().ToLowerInvariant().EndsWith(".jpg")|| 
                    path.Trim().ToLowerInvariant().EndsWith(".jpeg") 
                )) 
            { 
                SystemParametersInfoSetWallpaper(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); 
                RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); 
                switch (style) 
                { 
                    case WallpaperStyle.Stretch: 
                        key.SetValue(@"WallpaperStyle""2"); 
                        key.SetValue(@"TileWallpaper""0"); 
                        break; 
                    case WallpaperStyle.Center: 
                        key.SetValue(@"WallpaperStyle""1"); 
                        key.SetValue(@"TileWallpaper""0"); 
                        break; 
                    case WallpaperStyle.Tile: 
                        key.SetValue(@"WallpaperStyle""1"); 
                        key.SetValue(@"TileWallpaper""1"); 
                        break; 
                    case WallpaperStyle.NoChange: 
                        break; 
                } 
                key.Close(); 
            } 
        }

Examples

Example 01: Set the wallpaper to a random picture in a folder.
PS > Get-ChildItem "C:\Users\Public\Pictures\" | Get-Random | Set-OSCWallpaper

Example 02: Set the desktop background color to a random color.
PS> Set-OSCDesktopColor -R (Get-Random -minimum 0 -maximum 255)
-G (Get-Random -minimum 0 -maximum 255) -B (Get-Random -minimum 0 -maximum 255)

Example 03: Restore the default settings.
PS> Set-OSCDesktopDefault

Prerequisites
Windows PowerShell 2.0 
Windows Vista or higher versions
Additional Resources
Technical Resource: 
Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx
Add-Type
http://technet.microsoft.com/en-us/library/dd315241.aspx
SystemParametersInfo function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947(v=vs.85).aspx
SetSysColors function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724940(v=vs.85).aspx