Can someone help me explain what is happening here? Sorry if this is a basic question,
I simplified it from a pipeline expression I’m trying to write:
$foo = pwd
$cmd = "dir"
& $cmd $foo #Works
dir $foo #Works
& "dir $foo" #Error
*The term ‘dir C:\’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:2
+ & <<<< “dir $foo”*
dir pwd #Error
*Get-ChildItem : Cannot find path ‘C:\pwd’ because it does not exist.
At line:1 char:4
+ dir <<<< pwd*
I would expect all four of these to yield the same results
Expressions in quotes are interpreted as a single argument. In the third command, the shell is interpreting it as a request for the command “
dir C:\” with no arguments, rather than a request for the command “dir” with an argument of “C:\“.