Greedo
Greedo

Reputation: 5523

Show powershell message box when minimised

I have a PowerShell script which is monitoring my filesystem. I would like it to display a message box when a new file is detected in a certain folder.

However because I have my PowerShell script running minimized, the message box does not pop up as expected.

Is there a way to get just the message box to appear on top of all other windows, preferably selected, without un-minimising the PowerShell script?

I generate the message box with this code (although any kind of on screen notification would be good)

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path= "C:\Users\[...]"
    $watcher.Filter = "*.bin"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  
    Add-Type -AssemblyName PresentationFramework #reference for message box
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { [System.Windows.MessageBox]::Show('Binary received') #display msgbox
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 5}

Upvotes: 1

Views: 2250

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

To show a message box as an always on top dialog, you can use MessageBox.Show using ServiceNotification style for the dialog:

Add-Type -AssemblyName System.Windows.Forms

[System.Windows.Forms.MessageBox]::Show("Message","Title",`
    [System.Windows.Forms.MessageBoxButtons]::OK,`
    [System.Windows.Forms.MessageBoxIcon]::Information,`
    [System.Windows.Forms.MessageBoxDefaultButton]::Button1,`
    [System.Windows.Forms.MessageBoxOptions]::ServiceNotification)

Upvotes: 3

Related Questions