Make WPF windows topmost in Powershell ISE

This topic contains 2 replies, has 2 voices, and was last updated by  Mark 1 year, 5 months ago.

  • Author
  • #64137


    Here I have the same MessageBox function realized in Winforms and WPF. They show up as expected if I simply call them, but if I call them after a Get-Credential prompt as in this example they are hidden behind the ISE window. I'm running PS 5.0 on Windows 10. On Windows 7 and PS ISE 5.0 the Winforms version shows up topmost but WPF does not. I tried adding $Form.Focus() or $Window.Focus() with no effect. How can I ensure that the user gets error messages?

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName PresentationFramework
    function MessageBoxWF ($Message, $Title) {
    	    $Form = New-Object 'System.Windows.Forms.Form'
    	    $Form.TopMost = $true
    	    [System.Windows.Forms.MessageBox]::Show($Form, $Message, $Title, 0, 48)
    function MessageBoxWPF ($Message, $Title) {
            $Window = New-Object 'System.Windows.Window'
    	    $Window.TopMost = $true
    	    [System.Windows.MessageBox]::Show($Window, $Message, $Title, 'OK', 'Exclamation')
    $cred = Get-Credential 'User'
    MessageBoxWPF "Error message." "Error" > $null
  • #64162

    Dan Potter

    This has been a known issue with the ise for as long as I can remember. The workaround is to never use ise;-)

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName PresentationFramework


    $cred = get-credential


    $msgbox= New-Object -ComObject WScript.Shell


  • #64195


    Thanks Dan, that Windows Script Host popup method works like a charm is ISE both on Win 10 and Win 7. It even makes sound if you decorate it properly. Great!

You must be logged in to reply to this topic.