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.
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’.
Still wondering … were you able to validate my findings? Just want to make sure I aint smokin bad stuff
I have been known to make very stupid mistakes in the past.