Script scheduled task

Hi all,

It’s me, the scripting disaster again!

Perhaps someone can help me out with the following problem.
I’ve got this script, with a function which enables changing the current desktop wallpaper on the fly.

The Set-Wallpaper function (not created by myself) uses some embedded C# programming. More info about the function can be found here:

When I load the script in ISE and run it, it changes the wallpaper instantly.
The next thing I want for this, is to schedule it and run it invisible at the startup of my PC.
Now there are two options for scheduling a Powershell script that apparently make a world of difference.

  1. Run only when a user is logged on
  2. Run whether user is logged on or not

When I use the option 1. the script runs, changes the wallpaper, but unfortunately it shows an interactive script window flashing by.
However option 2. really is invisible, yet it doesn’t seem to change the wallpaper value for unknown reasons to me.

I tried building some troubleshooting log into the script, to pinpoint where it fails doing what it should:

"====================" |Out-File d:\test.txt
"Randomised Wallpaper:" |Out-File d:\test.txt -Append
($WallPaper).FullName |Out-File d:\test.txt -Append
"====================" |Out-File d:\test.txt -Append
"Create dummy reg value:" |Out-File d:\test.txt -Append
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name Test -Value test
"====================" |Out-File d:\test.txt -Append
"Current user:" |Out-File d:\test.txt -Append
[Environment]::UserName |Out-File d:\test.txt -Append
"====================" |Out-File d:\test.txt -Append
Set-Wallpaper -Path $WallPaper.FullName
"====================" |Out-File d:\test.txt -Append
"Registry value:" |Out-File d:\test.txt -Append
Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop\' -Name Wallpaper |Out-File d:\test.txt -Append
"====================" |Out-File d:\test.txt -Append

When I open the log.txt, I can see the following checks

  • It generates a random wallpaper
  • Current user is me, which is correct
  • It generates a dummy registry value, so permissions seem to be correct
  • Set-Wallpaper does not give feedback
  • Registry value remains unchanged

Why is the output different, when changing the “user logged on/not”-setting of the scheduled task? And why do my checks all give a positive result, except when executing the function inside the script?

If anyone got a clue about this one, please let me know!


Because if there’s no user logged on, there’s no wallpaper. Wallpaper is a user preference; no user = no preference = no love.

Don is right HKCU is not there until some one is logged in I have done some remote registry stuff using the user SID but if the user was not logged in and I used HKCU it will return my admin info from the registry rather then the user.

If that would be the case, than why:

Does this output shows me my own user account

[Environment]::UserName |Out-File d:\test.txt -Append

And why does this line succeeds in adding a value to my HKCU path:

# Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name Test -Value test

Both commands in the same script, give me the proper behaviour.
Does the plot thickens?

Also, on a sidenote:
Even though I’ve selected option 2. Run whether user is logged on or not, the scheduled task still is only triggered to run at log on.

Does it show your user account or your admin account you are using to run the task? Try this set up the task as a different user I bet it will attempt to set the keys as that person. What is happening is the task is logging in as you to run the script. Like my example above when I used the SID to retrieve remote registry values if the user was logged off.

I use the HKU: registry keys. I get the user SID from AD then use it to remotely access the user preferences. there the same as HKCU

$Computer = (Read-Host -Prompt "Computer Name")
$user = get-aduser (Read-Host -Prompt "User ID") -properties SID  -ErrorAction Stop
$reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('user', $Computer)
$keyPath = "$($user.Sid)\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
$RegKey= $Reg.OpenSubKey($keyPath)

read this

this line might help just change the User ID to the one you are trying to update
$SID = Get-WmiObject win32_useraccount -Filter “name = ‘User ID’” | Select SID

Mark and Don, thanks for your replies!

I just want this to work for my own (admin) account. I do not intend to use this remote or for other users. The thing that keeps bothering me, is that I can modify the HKCU-hive in the same script, but the Set-Wallpaper won’t work using the same scheduled task options.

My end-goal here, really is simply changing the wallpaper at startup without showing a Powershell window flashing by. I know…Windows has this option in the GUI. But I have a startup script that does a lot more with my wallpapers, allowing me to enjoy season themes without fiddling within the GUI.

A more simple and natively option would be this:

set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value ""
rundll32.exe user32.dll, UpdatePerUserSystemParameters

The rundll32 part, should update the user-profile, thus modifying the wallpaper visibly on the fly. Drawback is, that this doesn’t work 5 out of 10 times.

The Set-Wallpaper function, really does some magic with the C# programming in there. I’m just a nitpicker who wants it to run on boot, fully hidden.

But again, thanks for your help with this!


Figured it out and got it working like a charm.

I setup the task like this:

  • Trigger: At log on of any user (or assign a specific user)
  • Run only when user is logged on
  • Program/script: powershell.exe
  • Arguments: -NoProfile -WindowStyle Hidden “D:\WindowsPowerShell\WallPaperFun.ps1”

I was assuming wrongly that “WindowStyle Hidden” did not work. When you test-run a scheduled task from the Windows GUI, it apparently ignores the “WindowStyle” parameter and still shows a Powershell box popping up. However when testing by logging into Windows and allowing the script to run, the popup remains invisible.

Problem solved so it seems.

Thank you for your time and effort with this.

Happy scripting with lots of love,

Too soon. A reboot still shows the popup while running the script.

right tools for right things :slight_smile:
make SetDesktopWallpaper application for yourself and use it