Windows – Show balloon notifications from batch file

batch-filecmdpopup-balloonswindows

I would like to show balloon notifications from batches that run during long time.

I know you can show popups (and some solutions are very imaginative), but it would be interesting to have this option too.

Is there a way you can show a balloon notification from a batch file? Starting from Windows 7 at least please but XP welcome too.

I suppose it would necessary at least a tray icon but frankly I cannot go much farther. Maybe with rundll32.exe? I know this is not a recommended way to do anything from command line.

The function signature required for functions called by rundll32.exe
is documented in this Knowledge Base
article.

That hasn't stopped people from using rundll32 to call random
functions that weren't designed to be called by rundll32, like user32
LockWorkStation

or user32
ExitWindowsEx
.

But I found this RunAnyDll utility by Yaniv Pessach that appears to make the .dll calls right. Unfortunately, I found not a working link to test it (no, it isn't in the Internet archive). I've written the author to ask him for one. Let's see.

(I don't even know if it will be useful but it's an interesting utility. Maybe it could make work this answer to the popup question. I think the difference between the Run box and the command line execution has something to do with the calling convention thing in Raymond Chen post.)

Another unfindable utility to test is the deprecated eventtriggers but again I'm not sure if it would be of help.

Edit to clarify:

Both npocmaka and greg zakharov answers are very interesting. npocmaka's one is nearer to what I was asking because it produces a balloon notification. greg zakharov's uses a diferent technique but its outcome is very similar (in this case a text appearing at mouse pointer location). I've tested them in XP and both work (with .NET installed). Both have a timeout and I don't know how to keep the notification until the user clicks (just in case the user is out but you want to see it).

Ideally, my question was about getting something like this.

@echo off

echo Some long part job that goes well.

call :balloon "Report about above tasks" [optional parameters about icon, title, timeout, etc.]

echo More tasks but one fails
if errorlevel 1 call :balloon "Error this and that" [optional parameters]

echo This is the end of the tasks
goto :eof

:balloon

    echo If you don't want to make a temporary file
    echo then you must make one-liner calls to PowerShell, wscript or whatever.

exit /b

So being hybrid scripts the drawback I see is that it must be very difficult to insert this inside any batch. Generating an .exe for every balloon seems impracticable if you are going to reuse several times in your batch so, in the end, as greg zakharov states, it's more robust and flexible to generate just an .exe that accept parameters and reuse it (just as notifu). In the end, I suppose that using a :balloon subroutine that makes a one-liner powershell call is the best you can get (almost natively in XP because you need a download but you also need .net).

Best Answer

Using a PowerShell one-liner

No need to create a file, it works no matter what execution policy is. Thanks to Mark Dodsons that showed us the way to do this with PS and to npocmaka for the icons list.

Save as, for example, balloon.bat (of course, you can insert this as subroutine in any batch).

@echo off

if "%~1"=="" call :printhelp  & exit /b 1
setlocal

if "%~2"=="" (set Icon=Information) else (set Icon=%2)
powershell -Command "[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); $objNotifyIcon=New-Object System.Windows.Forms.NotifyIcon; $objNotifyIcon.BalloonTipText='%~1'; $objNotifyIcon.Icon=[system.drawing.systemicons]::%Icon%; $objNotifyIcon.BalloonTipTitle='%~3'; $objNotifyIcon.BalloonTipIcon='None'; $objNotifyIcon.Visible=$True; $objNotifyIcon.ShowBalloonTip(5000);"

endlocal
goto :eof

:printhelp
echo USAGE: %~n0 Text [Icon [Title]]
echo Icon can be: Application, Asterisk, Error, Exclamation, Hand, Information, Question, Shield, Warning or WinLogo
exit /b

If there's no second parameter it defaults to Information.

I suppose you can use mshta and vbscript or javascript, but I had no success until now.