11

I would like to display a message to the user by using CMD. The way I know to do so is by creating a .vbs (VBScript) file and execute it from CMD like this:

(echo MsgBox "Line 1" ^& vbCrLf ^& "Line 2",262192, "Title")> File.vbs

start File.vbs

But what I want to do is to display the message without creating any file, directly from CMD. Maybe by using a command to run VBScripts right from CMD.

  • Look up the msg command. You may not have it if you are running a Home edition of Windows, but you can add it by just copying and pasting msg.exe from a Pro installation. – wysiwyg Dec 18 '17 at 1:07
  • I would something a little bit more "universal", for all windows 10 versions if there is a way. – Bita Dec 18 '17 at 1:39
  • There’s always powershell too. Oft overlooked, even though Microsoft has clearly started the transition to eliminating cmd prompt. – Appleoddity Dec 18 '17 at 4:32
14

As suggested by @wysiwyg, on Pro/Business versions* of Windows you're looking for the Msg command. It's available in all modern versions of Windows (at least as far back as XP). To display a message to the currently logged on user, run the following:

msg %username% Your message here

The result looks like this: enter image description here

One consideration with this command is that you cannot customize the Titlebar text.

You can learn more about Msg on TechNet.


*If you need a solution that works on all editions of Windows, including Home, in my opinion the VBScript method you're already aware of is the simplest solution to this problem. It works on all modern versions and editions of Windows in their default configuration, and is easy to use.

  • Not entirely true. There was a dps message box command since windows came out. I believe it was removed in Windows 7 or 8 as it was viewed as a possible exploit. – Keltari Jan 30 '18 at 17:07
5

As others have said there is no way to do this purely with cmd but although that is the title of the question, it seems to me that the part without file is more important to you and under certain circumstances this might be possible.

With powershell it should be possible to run code without anyfile like this:

PowerShell -Command "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('Hello World')"

VBS does not allow the direct execution of code via cscript or wscript but mshta does. You can use it like this (if you need multiple lines use ':' as a delimiter):

mshta vbscript:Execute("msgbox ""Hello World"":close")

As it is ie based it might be affected by some gpo restrictions so if you have the choice powershell is probably the safer bet

2

I needed something similar to this today. Since I couldn't find an external dependency solution I liked since I'm not really a fan of VBScript/Powershell, I went ahead and wrote one:

https://github.com/cubiclesoft/messagebox-windows

The message boxes it produces are modal to the current console window. That is, the user can't do anything until they close the dialog. Also, the return code from the MessageBox() call is returned to the caller so that a script can react to whichever button was pressed. And, of course, it supports the full range of options to the MessageBox() Win32 API.

Adding an 80KB executable to the mix might not be everyone's cup of tea. It is statically linked against the VC++ runtime though and supports Unicode, which explains why the file is so large given the minimalistic nature of what it does. And there is a minor issue of not being able to easily pass in newlines from the command-line to display multiline messages. On a minor upside, it displays the dialog in less wall clock time than any of the other solutions presented so far.

Of course, if file size doesn't matter and dialogs create a too "in your face" user experience and/or don't require user interaction, I also ran into this nifty project:

https://www.paralint.com/projects/notifu/

Which displays a popup balloon in the status notification area of the screen using IUserNotification. The downside is that the Notifu executable runs just shy of 240KB. Another unfortunate example that COM creates unnecessary bloat and leaves me wishing that there was a simple, lightweight API for the feature.

For something cross-platform, there's zenity. It's mostly for Linux but there are Mac and Windows ports too. The Windows port of zenity is over 1MB in size because, well, GTK is quite bloated.

  • Not because it has to ship all of gtk internally while native Windows versions just pick up the user32/comctl DLLs that are already there? – grawity Sep 9 '18 at 19:57
  • Huh? Please clarify the question. – CubicleSoft Sep 9 '18 at 20:30
0

I have a way to do this with cmd, but it does basically include a file, but the file is deleted instantly after the vbs is executed. So it isn't really. You can do :

echo x=msgbox("Hey! Here is a message!",Type+Icon+Action,"Hey! Here is a title!") > %tmp%\tmp.vbs
cscript //nologo %tmp%\tmp.vbs
del %tmp%\tmp.vbs

Type+Icon+Action is just basic vbs, for a reference, here. https://therevisionist.org/wp-content/uploads/2016/03/vbscript_msgbox_chart-e1458059002411.jpg?x47750 The code that I shown you makes a file which has VBS code in it, then it executes it using cscript, then it deletes the file after it is executed making it look like it has been summoned by the cmd prompt. To make it look like that more, just use @echo off . Hope this helped!

0

additionaly, with external (non windows) executable, there's

nircmd infobox "msg" "title"

If you have access to vb6 compiler, then this "1 liner" produces a 5.5k executable:

sub main
  msgbox command
end sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.