How to Display a Msgbox directly from CMD
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.
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
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:
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.
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.
Additionaly, with an external (non-native Windows) executable, there's
nircmd infobox "msg" "title"
If you have access to a VB6 compiler, then this one-liner produces a 5.5k executable:
sub main
msgbox command
end sub
To display a message to a user via RTR I use the command below:
msg %username% your message
If you don't know who the user is on that machine you use * to send a message to every session on the server name.
msg * your message
ref. https://www.lifewire.com/msg-command-2618093