Dialog box opening behind other windows

Hi everyone,

I have a powershell script that I am executing via a scheduled task to prompt users to reboot their machines every night.  The script, prompt, and reboot process work just fine now but I am having one last issue with this script.

When the scheduled task executes the dialog box does not pop up above all other windows.  Instead it just has a flashing powershell icon on the taskbar which then brings up the dialog box if you click on it.

Is there a way to ensure that this popup box is displayed on top of all windows (brought to the foreground)?  For reference, the current code is below:

# DEFINE SECONDS TO WAIT FOR RESPONSE
$seconds_to_wait=900
# DEFINE REBOOT POSTPONE TIME IF USER CLICKS NO
$rebootWaitTime = 7200

# OPEN POPUP WINDOW WITH YES/NO PROMPT
$pop = New-Object -ComObject WScript.Shell
$answer = $pop.Popup("Do you want to reboot now?  If you click 'No' your computer will reboot automatically in 2 hours.  If you do not respond to this message within 15 minutes, your computer will reboot automatically.  Please remember to save your work.",$seconds_to_wait,"Nightly Reboot",4)

# IF NO ANSWER AFTER $seconds_to_wait REBOOT
if ($answer -eq "-1")
    {
        shutdown /r /f
    }
# IF USER CLICKS NO, WAIT $rebootWaitTime THEN REBOOT
elseif ($answer -eq "7")
    {
        Start-Sleep -Seconds $rebootWaitTime
        shutdown /r /f
    }
# IF USER CLICKS YES, REBOOT IMMEDIATELY
elseif ($answer -eq "6")
    {
        shutdown /r /f
    }


  • Edited by Cosmos_85 12 hours 17 minutes ago
June 25th, 2015 2:56pm

Try changing

$answer = $pop.Popup("Do you want to reboot now?  If you click 'No' your computer will reboot automatically in 2 hours.  If you do not respond to this message within 15 minutes, your computer will reboot automatically.  Please remember to save your work.",$seconds_to_wait,"Nightly Reboot",4)

To be

$answer = $pop.Popup("Do you want to reboot now?  If you click 'No' your computer will reboot automatically in 2 hours.  If you do not respond to this message within 15 minutes, your computer will reboot automatically.  Please remember to save your work.",$seconds_to_wait,"Nightly Reboot",4,"",4096)

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2015 3:08pm

Thanks for the suggestion.  This is what I get when I tried it:

Cannot find an overload for "Popup" and the argument count: "6".
At C:\Reboot_Dialog.ps1:9 char:1
+ $answer = $pop.Popup("Do you want to reboot now?  If you click 'No' your compute ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest


June 25th, 2015 5:16pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics