Rob van der Woude's Scripting Pages

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 (discontinued since Windows Vista)

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

In Windows NT 4, 2000 and XP:

NET SEND %ComputerName% This is a popup message.
Notes: (1) Though the NET command is still available in Windows Vista and later versions, its SEND option is discontinued.
For Pro versions of Windows XP, Vista, 7 and later, use the MSG command instead.
  (2) The NET SEND command requires the Messenger service; if this service is disabled, no messages will be displayed.
  (3) 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 (Windows 2000 Terminal Servers, Windows XP Pro and later versions)

In Windows 2000 Terminal Server sessions, Windows XP Pro, Vista, 7 or later use the MSG command to generate popup messages.

 

Alternative Scripting Languages (On-The-Fly)

KiXtart

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

 

VBScript (temporary script files generated on-the-fly)

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 Internet Explorer section.

 

VBScript (one-liners with MSHTA.EXE)

Simple VBScript code can be executed on-the-fly without the need for a temporary script file by using MSHTA.EXE, Windows' native HTA engine.

A simple MsgBox, OK only, no need to return a result:

:: Inspired by: https://lolbas-project.github.io/lolbas/Binaries/Mshta/
MSHTA.EXE vbscript:Close(MsgBox("The files have been copied successfully",vbOKOnly,"Good News"))

This is what the dialog box will look like:

Simple VBScript message box

A popup message with 10 seconds timeout:

:: Inspired by: https://lolbas-project.github.io/lolbas/Binaries/Mshta/
MSHTA.EXE vbscript:Close(CreateObject("WScript.Shell").Popup("Multiple lines are possible, a"^&vbCrLf^&"custom title, a timeout, and a"^&vbCrLf^&"selection of buttons and icons.",10,"Read this quickly",64))

This is what the popup dialog will look like:

Simple VBScript popup dialog

Returning a result requires writing it to a temporary batch file, since MSHTA is not a console program and does not feature return codes.
The resulting temporary batch file is then used to save the answer in an environment variable.

:: Inspired by: https://lolbas-project.github.io/lolbas/Binaries/Mshta/
:: Array of MsgBox button constants and captions
SET Button.1=OK
SET Button.2=Cancel
SET Button.3=Abort
SET Button.4=Retry
SET Button.5=Ignore
SET Button.6=Yes
SET Button.7=No
:: Generate a VBScript MsgBox and write the result to a temprary batch file
MSHTA.EXE vbscript:Close(CreateObject("Scripting.FileSystemObject").OpenTextFile("%~dpn0_tmp.bat",2,True).Write("@SET Answer=%%Button."^&MsgBox("Are you really sure?",vbYesNoCancel,"Please Confirm")^&"%%"))
:: Run the temporary batch file to save the answer to an environment variable
CALL "%~dpn0_tmp.bat"
ECHO You answered %Answer%
:: Delete the temporary batch file
DEL "%~dpn0_tmp.bat"

This is what the dialog box will look like:

MSHTA message box requiring an answer

Note: The argument for MSHTA.EXE must not be doublequoted, since a doublequoted argument is interpreted by MSHTA.EXE as a file name.
Prevent spaces in the argument, except within doublequoted strings, otherwise the argument will be clipped at the first space.

 

PowerShell one-liners

Now that PowerShell is available in all current Windows versions, its -Command switch may be used for dialogs.
Unlike VBScrip on-the-fly, PowerShell on-the-fly does not require temporary files.

A simple message, no result returned:

POWERSHELL.EXE -Command "Add-Type -AssemblyName System.Windows.Forms; [void] [System.Windows.Forms.MessageBox]::Show( 'Your password was changed successfully', 'Success', 'OK', 'Information' )"

This is what the dialog box will look like:

PowerShell simple message box

To return an answer we'll use a FOR /F loop with backquotes:

FOR /F "usebackq" %%A IN (`POWERSHELL.EXE -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show( 'Are you really sure?', 'Please Confirm', 'YesNoCancel', 'Question' )"`) DO SET Answer=%%A

This is what the dialog box will look like:

PowerShell message box requiring an answer

The result (caption of the button clicked) is stored in environment variable %Answer%.

Note: More dialogs can be found at my PowerShell code snippets page.

 

Internet Explorer (discontinued in 2022)

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.

Warning: As of June 15, 2022, support for Internet Explorer is discontinued, and in the near future Internet Explorer will be uninstalled by a Windows Update.
With this in mind, the Internet Explorer-based dialogs technique is no longer recommended!

An example using IELogin.vbs:

:: Note: delims is a TAB
FOR /F "tokens=1,2 delims=    " %%A IN ('CSCRIPT //NoLogo IELOGIN.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.

 

Related Stuff


page last modified: 2022-06-29; loaded in 0.0038 seconds