Displaying MsgBox TopMost

by Feb 19, 2013

In a previous tip you learned how to load additional .NET assemblies. This enables you to display dialog boxes like a MsgBox, pretty much like in VBScript. You can even make the MsgBox stay on top of all other windows so it never gets covered and hidden in the background:

PS> Add-Type -AssemblyName Microsoft.VisualBasic
PS> $result = [Microsoft.VisualBasic.Interaction]::MsgBox('My Message','OKOnly,SystemModal,Information', 'HeaderText')
PS> $result

Note the flag "SystemModal": it keeps the MsgBox dialog topmost so it cannot be covered by any other window. To display different buttons and/or icons, take a look at all of the flags you can combine as a comma-separated list:

PS> [System.Enum]::GetNames([Microsoft.VisualBasic.MsgBoxStyle])
ApplicationModal
DefaultButton1
OkOnly
OkCancel
AbortRetryIgnore
YesNoCancel
YesNo
RetryCancel
Critical
Question
Exclamation
Information
DefaultButton2
DefaultButton3
SystemModal
MsgBoxHelp
MsgBoxSetForeground
MsgBoxRight
MsgBoxRtlReading

Twitter This Tip! ReTweet this Tip!