Displaying a message box from the Windows command line

With no special software or compiling; just a little scripting.

message box created by script

When I run a time-consuming batch file that executes perl scripts or XSLT stylesheets on hundreds of files, I usually end the batch file with an echo command with only a Control-G as its output, so that a beep lets me know that the job is done. Processing some client files while watching Mark Birbeck speak at XML 2008, I knew it would be rude to have my computer emit such an obnoxious beep, so I found a nice alternative: a command line way to display a message box about my task being finished using only native Windows features.

First, I needed a short Windows JavaScript script like this, which I called msgbox.js:

if (WScript.Arguments.length < 1) {
    msg = "No message supplied"
}
else {
  msg = "";
  for (i = 0; i < WScript.Arguments.length; i++) {
      msg = msg + WScript.Arguments.Item(i) + " ";
  }
}
WScript.Echo(msg);

If it’s invoked with any arguments, it displays them as the text of a message box.

Then, I wrote this one-line batch file, which I called msgbox.bat, to call the JavaScript script:

wscript \util\msgbox.js %*

WScript is the more Windows-oriented sibling of CScript, the Windows JavaScript engine that I’ve written about before. They’re both included with Windows.

Now, if I end a batch file with this line,

msgbox Yo! The fixfiles.bat batch file is all done.

the message box shown above displays.

Of course there are dozens of other ways to display a message box, but it’s always nice to find a way to do something useful with minimal code and no downloaded or newly purchased software.

2 Comments

By gonzalo rodriguez on November 26, 2009 6:51 PM

Greetings:
Thank you very much for that information, did not know WScript.

By Alexander on January 25, 2010 5:38 AM

Hi, thanks for the post. I have looked for a fast and simple way to display a message box under Windows. And I have found it ! =)