How can I run a command + arguments inside a variable using powershell from cmd?
This is what works fine:
powershell -c " $test='dir'; &($test) "
But when I supply an argument to the command, for example:
dir /a -> powershell -c " $test='dir /a'; &($test) "
I get this error:
&: The term 'dir /a' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
In line:1 char:19
+ $test='dir /a'; &($test)
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir /a:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
This is what I tried but did not work:
powershell -c " $test='dir /a'; &($test) "
powershell -c " $test='dir /a'; Invoke-Expression '&$test' "
powershell -c " $test='dir /a'; Invoke-Expression """&($test)""" "
powershell -c " $test='dir /a'; Invoke-Expression '&($test)' "
powershell -c " $test='dir /a'; Invoke-Expression &($test) "
How can I execute the content of $test, without pipeing to cmd?