Using the daily Bing Wallpaper as Desktop background

Update 2015-01-19 : Arjan Mensch pointed out I used the wrong source to download the daily image, in the current function below Bing.com is used and the HD image is downloaded.

As you may or may not know Microsoft offers a daily wallpaper through www.bing.com, as I kinda like most of these wallpapers and dislike staring at a simple and the same wallpaper every day I started searching for an app or something that would use the current Bing.com wallpaper on my Desktop.

As you might guess by now I did not succeed in this quest to find a clean and simple method to do so, with this in mind I created a little Powershell script that downloads the current wallpaper and saves it to a specified folder. With another little function I change my current wallpaper to this new download wallpaper.

I used a previous post of mine to schedule this script and voila My Desktop changes every day.

The script:

function get-BingWallpaper
{
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$destinationPath,
[Parameter(Mandatory=$True,Position=2)]
[string]$destinationName

)
<#
.Synopsis
Downloads the current Bing Wallpaper from Bing.com.
.Description
Downloads Bing Wallpaper.
.Parameter destinationPath
The path where the Wallpaper will be stored.
.Parameter $destinationName
The file name to which the wallpaper will be stored.
.Example
Get-BingWallpaper -destinationPath “c:\Wallpapers” -destinationName “wallpaper.jpg”
.Link

.Notes
Name: Get-BingWallpaper
Author: Michael Verbeek
Lastedit: 19/01/2015
#>

$web = New-Object Net.WebClient

$baseUrl=”http://www.bing.com”
$jsonUrl=”http://www.bing.com/HPImageArchive.aspx?format=js&mbl=1&idx=0&n=1&cc=us”

$json=Invoke-WebRequest -Uri $jsonUrl | ConvertFrom-Json

$webclient = New-Object System.Net.WebClient

$errors = @()

$url = $baseUrl+$json.images.url.toString().trim();

# Requires “http://” for URL
if ($url.indexOf(“http://”) -eq -1) {
$url = “http://” + $url
}
if ($url -ne “”) {
$localImagePath = Join-Path $destinationPath $destinationName

try {
$webClient.DownloadFile($url, $localImagePath)
}
catch [Exception] {
$errors += $url
Write-Host “ERROR: ” + $_.Exception.ToString() -foregroundcolor “red”
}
}
Write-Host “done. errors: ”
$errors

return $localImagePath
}

 

function Set-Wallpaper
{
param(
[Parameter(Mandatory=$true)]
$Path,

[ValidateSet(‘Center’, ‘Stretch’)]
$Style = ‘Stretch’
)

<#
.Synopsis
Sets a specific wallpaper as Desktop Background.
.Description
Sets the desktop background.
.Parameter Path
The path where the Wallpaper is stored.
.Example
Set-Wallpaper -Path “c:\Wallpaper\Wallpaper.jpg”
.Link

.Notes
Name: Set-Wallpaper
Author: Michael Verbeek
Lastedit: 01/19/2015
#>

Add-Type @”
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Center, Stretch
}
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;
}
key.Close();
}
}
}
“@

[Wallpaper.Setter]::SetWallpaper( $Path, $Style )
}

Save this script as BingWallpaper.psm1.

The script exists of two functions, the first (Get-BingWallpaper) downloads the current wallpaper and saves it to the specified path. The second (Set-Wallpaper) set your current wallpaper to the specified wallpaper (this can be the wallpaper you downloaded with Get-BingWallpaper, but ik can be used to set your wallpaper to any JPG you specify.

Example:

Download the current Bing Wallpaper:
Set-BingWallpaper -destinationPath “c:\Wallpapers” -destinationName “Wallpaper.jpg” (mind that the destination folder needs to exist)

Set your background:
Set-Wallpaper -Path “c:\Wallpapers\Wallpaper.jpg”

The result will be:

Current www.bing.com:
bing

My Desktop after running the script:
Day1

The function can be use for multiple purpose for example changing the background of your RDWebAccess page like my colleague Arjan Mensch describes in his blog

http://msfreaks.wordpress.com/2015/01/14/rd-web-access-automate-bing-wallpaper-integration/

10 comments on “Using the daily Bing Wallpaper as Desktop background”

  1. fdfd says:

    How to running this script?

    im already save script with BingWallpaper.psm1
    then trying to run power shell with this command

    Set-BingWallpaper -destinationPath “c:\Wallpapers” -destinationName “Wallpaper.jpg

    and then

    Set-Wallpaper -Path “c:\Wallpapers\Wallpaper.jpg”

    and got error :

    PS C:\Windows\system32> Set-Wallpaper -Path “c:\Wallpapers\Wallpaper.jpg”
    Set-Wallpaper : The term ‘Set-Wallpaper’ is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + Set-Wallpaper -Path “c:\Wallpapers\Wallpaper.jpg”
    + ~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Set-Wallpaper:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    btw: im already make folder wallpapers on c drive and run power shell with admin privilages

    1. Michael says:

      Hi, first you need to import the script with the functions: import-module BingWallpaper.psm1

      1. fdfd says:

        Hi Michael,

        Thank you for response,
        got error when type import-module BingWallpaper.psm1

        PS C:\Windows\system32> import-module BingWallpaper.psm1
        import-module : The specified module ‘BingWallpaper.psm1’ was not loaded because no valid module file was found in any
        module directory.
        At line:1 char:1
        + import-module BingWallpaper.psm1
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : ResourceUnavailable: (BingWallpaper.psm1:String) [Import-Module], FileNotFoundException
        + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

        1. Michael says:

          Hi,

          first go to the folder where you saved the BingWallpaper.psm1

          for example cd c:\scripts

          then import-module BingWallpaper.psm1 or use the full path “import-module c:\scripts\BingWallpaper.psm1”

  2. Skip says:

    Hello, and I’m sorry but I’m not able to get this to work. I’m sure it’s due to my lack of PowerShell scripting experience, not the code.

    Can you please take a look at this image to see if you can determine what I might be doing wrong? I posted it to my little website, hope you don’t mind.

    http://skipphilip.me/?page_id=179

    1. Michael says:

      Hi Skip,

      Sorry for the late response busy days the last few weeks.

      The commands used in your image looks fine to me. Can you try to rename the psm1 to ps1 and try to import it again.

      1. Hernan says:

        Hi to everyone! I think that the correct command is “get-BingWallpaper -destinationPath …”

  3. Fantastic site. A lot of helpful information here.
    I am sending it to some pals ans also sharing
    in delicious. And of course, thank you for your sweat!

  4. Hda says:

    Very good I found this article interesting.

Leave a Reply to fdfd Cancel reply

Your email address will not be published. Required fields are marked *

captcha

Please enter the CAPTCHA text

This site uses Akismet to reduce spam. Learn how your comment data is processed.