Displaying Warning Dialog (Part 2)

by Aug 23, 2021

In the previous tip we created new shortcut files, and you have seen how the CreateShortcut() method provides methods to control almost any detail of a shortcut. Here’s the code again that creates a PowerShell shortcut on your Desktop:

Now here is a trick that may help you always display the dialog on top of other windows. For this, the code below creates a new dummy window and makes sure this window is positioned on top of all other windows. Then, this window is used as a parent for the popup dialog:

Add-Type -AssemblyName  System.Windows.Forms
$message = 'Your system will shutdown soon!'
$title = 'Alert'

# create a dummy window
$form = [System.Windows.Forms.Form]::new()
$form.TopMost = $true

# use the dummy window as parent for the dialog so it appears on top of it
[System.Windows.Forms.MessageBox]::Show($form, $message, $title, [System.Windows.Forms.MessageBoxButtons]::OKCancel, [System.Windows.Forms.MessageBoxIcon]::Warning)

# don't forget to dispose the dummy window after use
$form.Dispose()


Twitter This Tip! ReTweet this Tip!