--%
, the stop-parsing symbol, cannot be combined with PowerShell variables and expressions, because it passes the remainder of the command line through to the target executable - cmd.exe
in this case - as-is, the only exception being the expansion (interpolation) of cmd.exe
style environment-variable references, such as %USERNAME%
.
Generally, --%
comes with many limitations - see this answer.
Something like the following should work; note that &
must be quoted in order to be passed through to cmd.exe
, whereas >
, due to being unquoted, is interpeted by PowerShell.
function concat-filenames {
cmd.exe /c echo file $args[0] '&' echo file $args[0] > list.txt
}
Note that in cmd.exe
trailing spaces after an echo
command, before a subsequent operator (such as &
or >
), become part of the output.
If you meant to pass the single quotes as part of the value (as opposed to their syntactic use as a PowerShell string literal), use expandable strings ("..."
):
function concat-filenames {
cmd.exe /c echo file "'$($args[0])'" '&' echo file "'$($args[0])'" > list.txt
}
Note that in both cases PowerShell performs re-quoting on demand behind the scenes: if the expanded (interpolated) argument value contains spaces, it is wrapped in double quotes ("..."
).