Can someone explain the differences between Invoke-Expression $test
and Invoke-Expression -Command $test
?
variable test is:
$test = "notepad.exe myfile.txt"
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>]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With