14

Note: This is a question-with-answer in order to document a technique that others might find useful, and in order to perhaps become aware of others’ even better solutions. Do feel free to add critique or questions as comments. Also do feel free to add additional answers. :)


How can I display a messagebox by typing a single Windows command, e.g. in the Run dialog from the Start menu, or in the [cmd.exe] command interpreter?

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331

4 Answers4

28

One way is to use apparently undocumented functionality, namely that [mshta.exe], the runtime engine for Windows .hta HTML applications, accepts a general URL as command line argument, including a javascript: protocol URL:

mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'Message!', 10, 'Title!', 64 );close()"

            The resulting messagebox

This command can be issued in e.g. [cmd.exe]], or e.g. in the Run dialog from the Start menu, perhaps combined with the schtasks command to create a tea-timer…

The above messagebox times out after 10 seconds, but specifying a 0 second timeout means “don’t time out”, producing a more ordinary persistent messagebox.

For a simpler messagebox you can instead use the alert function provided by the MSHTA host.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 2
    well `mshta "javascript:alert('Message!');"` should also work then – Pierre Dec 10 '14 at 10:47
  • 1
    According to the [Popup Method documentation](https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx), it can show more buttons (like _Retry_ and _Cancel_) and can return the value of the button that has been pressed. How could this value be returned to the batch script? – Wayfarer May 31 '16 at 15:05
  • 1
    @Wayfarer; The natural way would be via the `mshta` process exit code, but I don't know any reasonable way of doing that (unreasonable way: killing the process). Alternatives then include storing the value in a registry key or a file. It's ugly and it's then no longer a one-liner. Instead, if I needed that functionality, I think I'd write a utility in C++. That's almost trivial. – Cheers and hth. - Alf May 31 '16 at 15:37
  • Ghetto reminder: at 16:30 mshta javascript:alert("Pick up kids from school!");close(); – Konrads Dec 02 '16 at 14:46
  • 1
    @Wayfarer, probably too late to help, but there's a way to get the exit code (errorlevel) here: https://stackoverflow.com/questions/20818111/how-to-return-exit-status-from-an-hta-vbscript-to-the-calling-batch-file – Sam Hasler Jan 26 '18 at 10:09
  • @Pierre, it works, except afte user presses `OK`, there is another window gets opened. This fixes it: `javascript:alert('Message!');close();` – FractalSpace Jul 12 '21 at 21:56
9

on command prompt:

msg %username% Message

interesting parameters are:

/w        (wait for user) 
/time:<seconds>
Stephan
  • 53,940
  • 10
  • 58
  • 91
1

Found that if you copy msg.exe from a Win7 Pro machine to a Win7 Home machine it works. Copy msg.exe to and from the C:\Windows\System32 folder.

  • An alternative, when one has the option of creating a file, is to just create VBScript or JScript script file. E.g. in file `msgbox.vbs` place this code: `msgbox WScript.Arguments(0), vbInformation, "Message!"`. This way a Win7 Pro installation is not required. – Cheers and hth. - Alf Mar 06 '15 at 17:13
1

What if you create a small VBScript with the message you want to display? I.e. create e file, named "Message.vbs" with the content:

MsgBox "Some info here", 0, "Message Title"

and call it like this:

cscript.exe PATH\Message.vbs

Panayotis
  • 1,792
  • 23
  • 32
  • Yes. For more generality one can change the first argument to `WScript.Arguments(0)`, as I noted [in a comment on another answer](http://stackoverflow.com/questions/21165565/windows-cmd-exe-command-to-display-a-messagebox-with-timeout/33963626#comment46067347_28903514). And for the second argument one can use `vbInformation` or its numerical equivalent. It's probably also a good idea to add the option equivalent of the C and C++ `MB_SETFOREGROUND`, lest the box is presented behind a console window; I don't know the VBS name for that, sorry. – Cheers and hth. - Alf Nov 27 '15 at 21:47