Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Invoke-Expression and Invoke-Expression -Command

Can someone explain the differences between Invoke-Expression $test and Invoke-Expression -Command $test?

variable test is:

 $test = "notepad.exe myfile.txt"
like image 405
rschirin Avatar asked Sep 05 '13 13:09

rschirin


2 Answers

Both Invoke-Expression $test and Invoke-Expression -Command $test are the same. Both will put $test into the 'command' parameter that is at position 1. The -Command is and optional parameter name that you can put in.

SYNTAX
    Invoke-Expression [-Command] <string> [<CommonParameters>]
like image 75
Richard Avatar answered Oct 14 '22 00:10

Richard


They're functionally equivalent. -Command is the only parameter the cmdlet takes that isn't in the CommonParameters set and the first one (by default, since it's the only one) when used positionally.

All you're doing with the second example is being explicit with naming your parameter, instead of relying upon position. That's a good habit to get into. Verbose, but future-proof and it makes your intention crystal clear.

like image 32
alroc Avatar answered Oct 14 '22 00:10

alroc