Message Box to Foreground (TopMost)

I cannot find a way to have my message box come to the front of the screen (on top of all other windows). I am launching the script as a Windows Scheduled Task via vbscript (the only way I could find to run the powershell script silently without an additional window coming up like cmd, powershell, etc). However, the message appears to come up BEHIND other windows. I cannot find a way to get the message to the front unless I don’t use a message box and just create a custom form with a message and the “TopMost” command. Thoughts/suggestions?

Add-Type -AssemblyName System.Windows.Forms

$result = [System.Windows.Forms.MessageBox]::Show("MESSAGE", "TITLE", 'YesNo', 'Warning')

If ($result -eq 'Yes'){DO SOMETHING}


I don’t think that’s possible.
This code:

$result = [System.Windows.Forms.MessageBox]::Show("MESSAGE", "TITLE", 'YesNo', 'Warning', "Button1", "RightAlign" )

Sets the YES button as default and right-aligns the text.
According to
this article an option “ServiceNotification” should set the MessageBox on top, however it doesn’t work (neither does the “DefaultDesktopOnly” option).
I’d create a message box myself, sorry.

Hold your horses: My .Net Framework is 4.7.03056, the article I linked to specifies 4.7.2 so maybe it’ll work on that version of .Net Framework.

By design, message box always open as the model window. So it will remain on top if your application is set to be the top most…

Therefore your can try these…

  1. Just before popping up the Messagebox just make your application got focus and set it to be top most… Than open your message box…

or …

  1. simply make your own Message box Dialog form and set the form as top most and than pop up that custom message box…

or … call the original Win32 MessageBox API:

public static extern int MessageBox(int hWnd, String text, String caption, uint type);

Once this is done you could include the MB_TOPMOST constant in the type to get it to be topmost.

or …

You can use comobject here, create a object to use the Appactive() method.

$App = New-Object -ComObject

I tried this already. However, it is not working. I am basically launching a hidden powershell script and then launching the message box. I have this listed before the message box section:

(New-Object -ComObject WScript.Shell).AppActivate((get-process powershell -ea SilentlyContinue).MainWindowTitle)

This vbscript method MIGHT work. It has been the only option that has had positive results with coming to the front of the screen…

Add-Type -AssemblyName Microsoft.VisualBasic

$result = [Microsoft.VisualBasic.Interaction]::MsgBox('MESSAGE','YesNo,SystemModal,Information', 'TITLE')