The coolest thing about Bing isn’t the search or OS integration. It is the gorgeous search backgrounds they have. Did you know that you can take that Bing background and have it replace the default Windows Logon background?
In this guide, we are going to use PowerShell and Group Policy to automatically download the Bing picture of the day and deploy it to our computers! Everyday, the logon background will update and will look like the screenshot below.
Using PowerShell to Download the Bing Background
This script connects to a Bing Images RSS feed, downloads the first picture and saves it to a network location. You will need to change line 6 to your network share. Ensure that the network folder exists and that domain computers have Read/Execute to it. The downloaded image will need to be named backgroundDefault.jpg.
$url = “http://feeds.feedburner.com/bingimages” $wc = New-Object net.webclient $xml = [ xml ]$wc.DownloadString($url) $xmlrss = $xml.rss.channel.item | Select-Object –first 1 $imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’) $savelocation = ‘\\dc\Background\Bing\backgroundDefault.jpg’ $wc.DownloadFile($imgurl,$savelocation);
Use a scheduled task to set this script to run daily at 5:30 AM local time. The command in your task will be powershell.exe and the parameters will be -noprofile “C:\Scripts\Bingbackground.ps1” . Thank you to Girish for writing this script!
Setting the Logon Background with Group Policy
In the Group Policy Management Console, create a new GPO named Bing Backgrounds. Under Computer Configuration/Preferences/Windows Settings/Files, create a new file preference and set the action to create. For the source, specify your save location from above (ex: \\SERVER\SHARE\Bing\backgroundDefault.jpg). For the destination, enter C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg .
Under the Common tab, check Apply Once and Do Not Reapply. Copy this file preference and then paste it to create a duplicate. Change the action to Replace and uncheck Apply Once and Do not Reapply. Select Item Level Targeting and add a time value target. In our environment, I only allow the picture to copy down from 5:00 AM to 9:00 AM. This prevents some network bloat. You should now have two file preferences listed.
Create a new Registry Preference under Computer Configuration. Leave the preference action at update. Enter in the following:
- Key Path: Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background
- Value Name: OEMBackground
- Value Type: REG_DWORD
- Value Data: 00000001
Under the Common tab, add a new Item Level Target that applies this registry change to just your Windows 7 machines. Now navigate to Computer Configuration/Policies/Administrative Templates/Control Panel/Personalization. Enable Force a specific default lock screen image and enter C:\windows\System32\oobe\info\backgrounds\backgroundDefault.jpg as the lock screen path. This will allow your Windows 8+ machines to have the Bing background!
Link your GPO to an OU containing computers. On the next GPUpdate, your computers will set the background registry key and copy down the Bing background. On the next reboot (or logoff), the default logon background will be set to the Bing background of the day! Next project – setting the logon screensaver for distributed announcements.
Hello Quim,
As from the details; i can see that on you side its like this: $xml = [xml][/xml]$wc.DownloadString($url)
We only need it to be as $xml = [xml][/xml]$wc.DownloadString($url) with no slash and brackets for single xml.
I’ve the same issue that someone before:
No se puede llamar a un método en una expresión con valor NULL.
En C:\descargarfondo.ps1: 5 Carácter: 2
+ $imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Excepción al llamar a “DownloadFile” con los argumentos “2”: “El valor no
puede ser nulo.
Nombre del parámetro: address”
En C:\descargarfondo.ps1: 7 Carácter: 1
+ $wc.DownloadFile($imgurl,$savelocation);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
What can I do to run the script correctly ?
Thanks a lot Alan,
It works perfectly.
rgds
Mehsar
Is there a reason you copy this locally instead of just calling it from the network share?
More aimed at Windows 8 im assuming you did it with 7 because the jpg needs to be called locally?
It started out originally for Windows 7 machines and was modified for 8. Storing it on the network would be a better solution if your clients are stationary and you are running Windows 8 mainly.
I am using PowerShell V4 and am getting the following error:
You cannot call a method on a null-valued expression.
At C:\~~~~\~~~~p\Bingbackground.ps1:5 char:1
+ $imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling “DownloadFile” with “2” argument(s): “Value cannot be null.
Parameter name: address”
At C:\~~~~\~~~~p\Bingbackground.ps1:7 char:1
+ $wc.DownloadFile($imgurl,$savelocation);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Any suggestions as to what I need to correct?
Can the computer that you are running the script on access: http://feeds.feedburner.com/bingimages
Actually I got the script to work (I posted a reply, not sure why it is not appearing).
The script works, but the GPO does not. Creating the GPO on a Windows 2008 R2 server for a Windows 7 64 Pro system.
Spent 6 hours on the phone with Microsoft, they can’t get it to work either.
Your first comment got marked as spam for some reason. That was probably why.
Are clients actually applying the GPO? Is the registry key getting created? Is the file copied locally and replacing the existing file?
The GPO is being applied, I am not certain if the Registry is getting created (I think not) and the file is Not getting copied.
Again, I spent 6 hours on the phone with NS Support and they could not figure out why either. They said the GPO is being applied but it is not working. Then they passed me off to another department.
I did this on a 2008 R2 domain but had 7 X64 enterprise instead of Pro.
Hello,
I haven’t been able to make the script work. Can you please post the full corrected script or guide the proper corrections please.
Rgds
Mehsar
$url = “http://feeds.feedburner.com/bingimages”
$wc = New-Object net.webclient
$xml =
$wc.DownloadString($url)
$xmlrss = $xml.rss.channel.item | Select-Object –first 1
$imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
$savelocation = ‘\dcBackgroundBingbackgroundDefault.jpg’
$wc.DownloadFile($imgurl,$savelocation);
Thanks Alan – I edited the post with the corrections.
What did you do to get the script working? I have Windows 7 Powershell 4, I’ve set the execution policy to Unrestricted and I still get the ‘null-valued expression’ error. I even went line by line through the script echoing the variables each time to see what happens and this line:
$xmlrss = $xml.rss.channel.item | Select-Object –first 1
produces a null value even though $xml is a huge rss feed blob where I can successfully locate the first instances of rss, channel and item tags.
Why?
Hello Jim,
Have you tried to insert logging in the script in order to figure out where it’s blocking?
Here’s my script which works perfectly:
$url = “http://feeds.feedburner.com/bingimages”
$wc = New-Object net.webclient
$xml = [xml][/xml]$wc.DownloadString($url)
$xmlrss = $xml.rss.channel.item | Select-Object –first 1
$imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
$savelocation = ‘C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg’
$wc.DownloadFile($imgurl,$savelocation);
Do you confirm that with your script; it doesn’t even download? or it doesn’t activate it as logon background?
For my case; the download was successful but the application as Logon Background wasn’t working; so I used the default location for saving without modifying Group Policy.
One point to consider is that; it doesn’t works the same way for Windows 8 and Windows8.1 as the former use AppData as temporary location for the image while the latter doesn’t.
Regards
Mehsar
I’m having the same issue than Jim. This is what happens when I run the script in Windows 8.1:
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. Todos los derechos reservados.
PS C:\WINDOWS\system32> $url = “http://feeds.feedburner.com/bingimages”
PS C:\WINDOWS\system32> $wc = New-Object net.webclient
PS C:\WINDOWS\system32> $xml = $wc.DownloadString($url)
PS C:\WINDOWS\system32> $xmlrss = $xml.rss.channel.item | Select-Object -first 1
PS C:\WINDOWS\system32> $imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
No se puede llamar a un método en una expresión con valor NULL.
En línea: 1 Carácter: 1
+ $imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS C:\WINDOWS\system32> $savelocation = ‘C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg’
PS C:\WINDOWS\system32> $wc.DownloadFile($imgurl,$savelocation);
Excepción al llamar a “DownloadFile” con los argumentos “2”: “El valor no puede ser nulo.
Nombre del parámetro: address”
En línea: 1 Carácter: 1
+ $wc.DownloadFile($imgurl,$savelocation);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Hi Mehsar,
I’ve tried your recommendation but it doesn’t work. Here’s the error message:
PS C:\WINDOWS\system32> $xml = [xml][/xml]$wc.DownloadString($url)
En línea: 1 Carácter: 14
+ $xml = [xml][/xml]$wc.DownloadString($url)
+ ~
Falta el nombre de tipo después de ‘[‘.
En línea: 1 Carácter: 13
+ $xml = [xml][/xml]$wc.DownloadString($url)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
Token ‘[/xml]$wc.DownloadString’ inesperado en la expresión o la instrucción.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingTypename
Mehar,
As I stated, all works well through the line beginning with $xml. This variable is filled with XML code like so:
Bing images
Daily background pictures from Bing.com
http://www.istartedsomething.com/bingimages/
Thu, 07 Aug 2014 14:32:44 +0000
Thu, 07 Aug 2014 14:32:44 +0000
(Note that my excerpt ends with the first tag.) The next script line:
$xmlrss = $xml.rss.channel.item | Select-Object –first 1
presumably loads $xmlrss with the contents of the first instance of the tag,
produces an empty variable, hence the retrieval of the image url from within fails with the null expression error. I just don’t understand why $xmlrss is empty.
Whoops, it looks like tags are removed/interpreted in replies. My reply looks weird.
Can someone upload the script to a website to download it ? Because I’ve tried everything in different OS and I can’t understand why isn’t working. It seems extremely easy.
Figured it out after having to leave it for a while. The issue is the deployhappiness feeddback interface. It removes text that looks like bracketed tags.
What worked is declaring the $xml variable with the upfront xml tag. When one passes the script into this site’s interface the tag is removed so the script appears wrong. So, in summary, the $xml variable line should read:
$xml = [ x m l ]$wc.DownloadString($url)
where all spaces in [ x m l ] above have been removed.
I hope this reply makes it into the interface unharmed.
Thank you for pointing that out Jim! I have a script plugin here that tries to format scripts. [XML] was certainly throwing it for a loop.
I edited the original post to fix the script.
We just posted and even didn’t verify that the brackets we’re automatically removed.
Well found Jim.
Thanks
Mehsar
I’m getting the same problem – what was the fix?
Fixed it – Correct line is
$xml =
$wc.DownloadString($url)
How yo corrected the issue ? I have the same problem that you.
Hello Quim,
I’ve seen that everyone is declaring the xml syntax as:
$xml = $wc.DownloadString($url)
The fact is that when declaring the syntax as $xml = [xml][/xml]$wc.DownloadString($url) automatically designs it in the proper way for powershell to interpret it.
Try it.
Regards
Mehsar
Hello,
The script that worked for me is:
$url = “http://feeds.feedburner.com/bingimages”
$wc = New-Object net.webclient
$xml = [xml][/xml]$wc.DownloadString($url)
$xmlrss = $xml.rss.channel.item | Select-Object –first 1
$imgurl = $xmlrss.selectSingleNode(‘enclosure’).getAttribute(‘url’)
$savelocation = ‘D:\backgroundDefault.jpg’
$wc.DownloadFile($imgurl,$savelocation);
A small correction in the line $xml = [xml][/xml]$wc.DownloadString($url) ;
I took much time in figuring out 😉
That the taste of powershell; mastering something we never know.
Regards
Mehsar
This script just does’t work for me.
I get an error
Missing expression after unary operator ‘-‘.
At line:1 char:2
+ – <<<< noprofile .\Bingbackground.ps1
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
What version of powershell are you running? 2 or 3 or 4?
2
Got it I had to exectue the follow to allow scripts to run
set-executionpolicy remotesigned
Awesome! Glad you got it figured out!
I manually ran this script and didn’t get a background downloaded. Did I do something wrong?
You will need to log off and log back in to see the changes. Have you done that? If so, make sure that you have the registry keys set as well and the file copied down to the local machine.
Great idea 🙂
Thank you Paul! Let me know if you have any issues getting it setup.
This is great!!. I have it implemented in a test OU, and my boss thinks its cool, so I am hoping he lets me put it on all the computers!!
As always, thanks for adding cool stuff for us!!
Matt
Thanks Matt! I am glad you like it!!