2

I try to run a simple powershell command by setting a variable and printing it.

This is what I want to do:

powershell -command "& {$name=\"hi\"; echo $name}"

But it fails with:

The string is missing the terminator: ".
   + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
   + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

The invoke operator (&) works fine with commands such as:

 powershell -command "& {&echo hi}"

I read about the invoking operator and how to execute commands with -command option and executing scripts with -File option etc. They are working as expected. But my attempts to do the same for setting a variable and printing it as above doesn't work. I suspect -command works with only commands. Any idea how to achieve what I do above?

1 Answer 1

3

from a DOS shell this works:

powershell -command "& {$name='hi'; echo $name}"

but also your code works.

From a Powershell console use this:

powershell -command {$name='hi'; echo $name}
Sign up to request clarification or add additional context in comments.

1 Comment

My bad! My actual command is bit more complicated. What I posted was a simplified example. I had the invoking operator inside the command block which caused the problem. Thanks for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.