1

Would appreciate it if someone helps me with the small task . I want to pass a command line arguments to a powershell script(that is not a file ).

Suppose , i just want to pass an argument that messagebox will show.

powershell.exe -command  "$str=$args[0];[System.Reflection.Assembly]::
LoadWithPartialName('System.Windows.Forms');
[System.Windows.Forms.Messagebox]::Show($str);" 'foo'

I want foo to be shown in messagebox. I get empty messagebox. Perhaps i missed something small.

5
  • 3
    instead of -command "code" arg, do -command "& {code} arg" Commented Jul 22, 2017 at 9:38
  • thanks Mathias, it works!!! Commented Jul 22, 2017 at 9:42
  • @MathiasR.Jessen please write your comment as answer, so nobody else waste time. Commented Jul 23, 2017 at 15:56
  • @JPBlanc done.! Commented Jul 23, 2017 at 16:07
  • @MathiasR.JessenThanks Commented Jul 24, 2017 at 4:48

1 Answer 1

2

Wrap the entire thing in a scriptblock {} and use the the call operator & to invoke it with an argument:

powershell.exe -command "& {$str=$args[0];[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');[System.Windows.Forms.Messagebox]::Show($str)} 'foo'"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.