MessageBox dialog not behaving as expected

I have a script that tends to always display message box dialog behind other windows. So, I tried using the DefaultDesktopOnly Property which is supposed to do what I want, display in the foreground. However, I cant get this to work in Windows Powershell (5.1). It works fine in Powershell 7.3.8 and PowerShell 2. Here is a simple script to demonstrate what I am trying to do.

Add-Type -AssemblyName PresentationFramework
$msgBox = [System.Windows.MessageBox]
$msgBoxResult = $msgBox::Show('Attention user, make a choice !!', 'Attention', 'YesNo', 'Question', 'Yes', 'DefaultDesktopOnly')

$msgBoxResult

When this is executed in PS 5.1, it falls right through and displays ‘No’ as the result without ever displaying the dialog. On PS 7 and PS 2, it does as expected and displays the dialog with the result being either Yes or No based on my choice. I have tried this on multiple systems so it is not a system issue. I have also tried using

$msgBox = [Windows.Forms.MessageBox]

Which has a slightly different syntax for the default button (Button1, Button2 etc versus Yes, No) and it behaves the same. Can someone please point me to the error of my ways? And yes, I did google this and based on that search, DefaultDesktopOnly should do what I want. I also tried ServiceNotification which behaves the same way.

Thanks in advance for any help you can provide.

Here is where I got my information:

My suggestion is to look at the New-MessageBox function in my PoshFunctions module on the PowerShell Gallery. It provides a way of displaying the message box and provides parameters to use certain options of the .Net function.

For instance if you ran the command
New-MessageBox -Message 'Choose' -Title 'Title info' -Buttons OKCancel -Icon Error -DefaultButton Button2 -AsString

And hit [Enter] it would return the string ‘Cancel’.

Thanks for the reply Bill. Sadly, I am unable to use external modules and don’t laugh, I need to support PowerShell 2 as well.

Were you able to validate my findings? Just curious.

<SCNR>

:scream: :scream: :scream:

</SCNR> :wink: :love_you_gesture:t3:

The function should work with Powershell 2. You will just have to dot source the one file found in

<modulepath>\Functions\New-MessageBox.ps1

Still wondering … were you able to validate my findings? Just want to make sure I aint smokin bad stuff :smirk:
I have been known to make very stupid mistakes in the past.

I confirmed exactly the same. Powershell 7 and 2 it works fine. Same issue with ServiceNotification

I offer an alternative solution

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
$msgBox = [System.Windows.MessageBox]
$window = New-Object System.Windows.Window
$window.Topmost = $true
$msgBoxResult = $msgBox::Show($window,'Attention user, make a choice !!', 'Attention', 'YesNo', 'Question', 'Yes')

$msgBoxResult
1 Like

Thanks Doug for both the validation and the solution !! Works perfect :slight_smile:

1 Like