How to Display a Msgbox directly from CMD

140,575

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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
Share:
140,575

Related videos on Youtube

Bita
Author by

Bita

Updated on September 18, 2022

Comments

  • Bita
    Bita over 1 year

    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.

    • wysiwyg
      wysiwyg over 6 years
      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.
    • Bita
      Bita over 6 years
      I would something a little bit more "universal", for all windows 10 versions if there is a way.
    • Appleoddity
      Appleoddity over 6 years
      There’s always powershell too. Oft overlooked, even though Microsoft has clearly started the transition to eliminating cmd prompt.
  • Keltari
    Keltari over 6 years
    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.
  • user1686
    user1686 over 5 years
    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?
  • CubicleSoft
    CubicleSoft over 5 years
    Huh? Please clarify the question.
  • Jason
    Jason over 4 years
    In addition to the above warning about mshta, some antivirus software considers it a potentially unwanted process. At work, my attempt to run it resulted in an overzealous enterprise product (SentinelOne) to quarantine my entire machine and remove it form the network.