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
powershell
public static void SetWallpaper(string path, WallpaperStyle style)
{
if (path == string.Empty )
{
RemoveWallpaper();
}
else if (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();
}
}
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