# Message Box to Foreground (TopMost)

**URL:** <https://forums.powershell.org/t/message-box-to-foreground-topmost/11396>\
**Category:** PowerShell Help\
**Created:** [October 1, 2018, 12:05pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396 "2018-10-01T12:05:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-toy](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@jeff-toy](https://forums.powershell.org/u/jeff-toy)\
**Post date:** [October 1, 2018, 12:05pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/1 "2018-10-01T12:05:20Z")

</div>

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}
```

Thanks!

---

<div class="post-metadata">

**Author:** ![krisvg](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@krisvg](https://forums.powershell.org/u/krisvg)\
**Post date:** [October 1, 2018, 12:53pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/2 "2018-10-01T12:53:55Z")

</div>

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](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messageboxoptions?view=netframework-4.7.2) 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.

---

<div class="post-metadata">

**Author:** ![krisvg](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@krisvg](https://forums.powershell.org/u/krisvg)\
**Post date:** [October 1, 2018, 1:03pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/3 "2018-10-01T13:03:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [October 1, 2018, 2:53pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/4 "2018-10-01T14:53:08Z")

</div>

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:

```
[DllImport("user32.dll")]
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 …

> http://community.idera.com/powershell/powertips/b/tips/posts/displaying-msgbox-topmost

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 1, 2018, 2:57pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/5 "2018-10-01T14:57:31Z")

</div>

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

```
$App = New-Object -ComObject wscript.shell
$App.AppActivate('TTILE')
```

---

<div class="post-metadata">

**Author:** ![jeff-toy](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@jeff-toy](https://forums.powershell.org/u/jeff-toy)\
**Post date:** [October 1, 2018, 3:52pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/6 "2018-10-01T15:52:23Z")

</div>

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')

$result
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/message-box-to-foreground-topmost/11396/7 "2024-05-16T20:34:17Z")

</div>


