Search code examples
c#.netpowershellmessagebox

MessageBox in Powershell not brought to front


I'm using an application which allows me to run Powershell scripts on devices within my network and I need to prompt users with a MessageBox.

My script creates the MessageBox fine, but my issue is that it always displays behind my application. I tried a solution online which suggested creating a new form with property Topmost=true and passing it as the first parameter, however it didn't seem to work. Is there anything immediately obvious that I'm doing wrong?

Add-Type -AssemblyName PresentationCore,PresentationFramework

$top = new-Object System.Windows.Forms.Form -property @{Topmost=$true}
$Result = [System.Windows.Forms.MessageBox]::Show($top, $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

Solution

  • Spawned windows need a parent. If they don't they will fall behind other windows (as you've found).

    The following isn't PowerShell related, per se ... but it's what you need to know/do to get where you want to go...

    Why isn't MessageBox TopMost?

    and possibly

    In Powershell how do I bring a messagebox to the foreground, and change focus to a button in the message box