Differences between Invoke-Expression and Invoke-Expression -Command
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.