User Output

How to Generate Popup Messages in Batch Files

Most messages in batch files consist of plain text in the command prompt.
We use ECHO to display a message text, and PAUSE or SET /P to wait for confirmation.

Sometimes, however, we would rather have a popup message that sits there waiting for confirmation by the user.

 

NET SEND

By far the easiest solution using native commands is NET SEND.

In NT 4 and later:

NET SEND %ComputerName% This is a popup message.
Notes: (1) The NET SEND command requires the Messenger service; if this service is disabled, no messages will be displayed.
  (2) By default the %ComputerName% environment variable is not available in Windows 9x or the "real" MS-DOS versions.
Use NetSetxx.bat to add this variable in Windows 98.

Using carets for escape characters we can even make multi line messages in NT 4 and later:

NET SEND %ComputerName% This is line 1 of the popup message.ˆ

This is line 2.ˆ

And this is line 3.

Note the empty lines following the carets, these are required to make this trick work.

This code will result in a message box similar to this one:

Multi line message box with NET SEND command

This method can also be used with the ECHO command:

ECHO This is line 1 of the popup message.ˆ

This is line 2.ˆ

And this is line 3.

This code will result in the following text on screen:

This is line 1 of the popup message.
This is line 2.
And this is line 3.

I wouldn't recommend it, though, as it doesn't make the batch file any easier to read.
Better use 3 ECHO command in a row to display the 3 lines.

By the way, with NET SEND, you can get a similar result using ASCII character 20 or ¶ (trick found in the PCReview Forum Archives):

NET SEND %ComputerName% This is line 1 of the popup message.¶This is line 2.¶And this is line 3.

Multi line message box with NET SEND command

This may even work in Windows 95/98/ME, but I didn't test that.

This trick does not work with the ECHO command:

ECHO This is line 1 of the popup message.¶This is line 2.¶And this is line 3.

This code will result in the following text on screen:

This is line 1 of the popup message.¶This is line 2.¶And this is line 3.

Quite useless, unfortunately.

Notes: (1) In Windows 9x escape characters are not available.
  (2) When using * instead of %ComputerName% to send a message to every computer in the network, the message length is limited to 128 characters.
Longer messages will be truncated.

MSG.EXE (Terminal Servers only)

In Terminal Server sessions, use the MSG command to generate popup messages.

Alternative Scripting Languages (On-The-Fly)

If you don't mind using a mix of scripting languages, consider creating a temporary KiXtart script on the fly, using the MessageBox( ) function to generate your popup message:

>  usermessage.kix ECHO $MsgStr = "Multiple lines?@CRLF" + "No problem"
>> usermessage.kix ECHO $Title = "My Personal Message Box"
>> usermessage.kix ECHO $X = MessageBoxˆ( $MsgStr, $Title, 0 ˆ)
KIX32.EXE usermessage.kix
DEL usermessage.kix

This code will result in a message box similar to this one:

KiXtart message box

 

If you prefer a native scripting language (Windows 2000 and later), create a VBScript script on the fly, using the WScript.Echo method (requires WSCRIPT instead of CSCRIPT):

> usermessage.vbs ECHO WScript.Echoˆ( "Multiple lines?" ˆ& vbCrLf ˆ& "OK..." ˆ)
WSCRIPT.EXE usermessage.vbs
DEL usermessage.vbs

This code will result in a message box similar to this one:

VBScript multi line message box

 

With a little more code you can customize the message title, add an icon and set a timeout period:

> usermessage.vbs ECHO Set wshShell = CreateObject( "WScript.Shell" )
>>usermessage.vbs ECHO wshShell.Popup "Multiple lines are possible, a" ˆ& vbCrLf ˆ& _
>>usermessage.vbs ECHO                "custom title, a timeout, and a" ˆ& vbCrLf ˆ& _
>>usermessage.vbs ECHO                "selection of buttons and icons.", 10, _
>>usermessage.vbs ECHO                "Read this quickly", 64
WSCRIPT.EXE usermessage.vbs
DEL usermessage.vbs

This code will result in a message box similar to this one, which will close automatically after 10 seconds:

VBScript multi line message box using Popup function

Message boxes asking for user input are possible too:

> usermessage.vbs ECHO WScript.Echo InputBox( "Where were you born?", "Place of Birth", "London" )
FOR /F "tokens=*" %%A IN ('CSCRIPT.EXE //NoLogo usermessage.vbs') DO SET PlaceOfBirth=%%A
ECHO You were born in %PlaceOfBirth%
DEL usermessage.vbs

This code will result in a message box similar to this one:

VBScript message box asking for input

Unfortunately, combining these input and output dialogs into a single dialog box isn't possible, to do that read the next section.

Internet Explorer

More advanced message boxes, input, output and any combination, including (masked) password prompts, can be created using VBScript and Internet Explorer.
However, I would not recommend creating them on the fly.

An example using IELogin2.vbs:

:: Note: delims is a TAB
FOR /F "tokens=1,2 delims=    " %%A IN ('CSCRIPT //NoLogo IELOGIN2.VBS') DO (
    SET Name=%%˜A
    SET Password=%%˜B
)
ECHO The password of %Name% is %Password%

This is what the dialog box will look like:

Sample login dialog created using VBScript and Internet Explorer

See the VBScript Scripting Techniques section for other advanced user message dialogs.
You may need to modify the first lines of code to display the results all on one line, as is the case in IELogin2.vbs.

Related Stuff


page last uploaded: 4 March 2011, 12:54