Wpftutorials Wpf
Wpftutorials Wpf

Reputation: 33

Powershell run message box from cmd

I am trying to achieve the following :

I need to run from batch file psh script , but not from file , solo de command line. I've tried the following code from ps console and it works , but when passing this from cmd - not.

powershell.exe -ExecutionPolicy Bypass -Command {[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('We are proceeding with next step.')}

Can anyone pls help to figure out la problema ? Gracias.

Upvotes: 3

Views: 2948

Answers (4)

js2010
js2010

Reputation: 27428

With using statements:

powershell "using assembly system.windows.forms; using namespace system.windows.forms; [messagebox]::show('hi there')"

Upvotes: 0

CoveGeek
CoveGeek

Reputation: 435

Great answer John. I would like to expand on it with a slightly different method.

If you look down in the comments section for the following link to the Scripting Guys page on this question, there is a great reference to a single command message popup.

Based on JohnLBevan's answer and code from Dan Hayward's comment 1 on the page below, I assembled and tested a very quick command line that supresses both the command from being echoed and powershell output from appearing in the console window.

@powershell.exe -ExecutionPolicy Bypass -Command "(new-object -ComObject wscript.shell).Popup(Popup message', 0, 'Popup Title')" 1> nul 2>&1

Scripting Guy: PowerTip: Use PowerShell to Display Pop-Up Window

Upvotes: 1

Stephen Connolly
Stephen Connolly

Reputation: 1639

Iirc you have to run power shell in a specific threaded apartment mode to get win forms to run properly so you will need to explicitly specify the threading mode using the relevant switch on the powershell.exe command line.

Upvotes: 0

JohnLBevan
JohnLBevan

Reputation: 24410

Switch {brackets} for "quotes":

@powershell.exe -ExecutionPolicy Bypass -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('We are proceeding with next step.')"

(also added the @ for echo off)

Upvotes: 4

Related Questions