11

I want to evaluate the content from StdIn in PowerShell, like this:

echo "echo 12;" | powershell -noprofile -noninteractive -command "$input | iex"

Output:

echo 12;

Unfortunately, $input is not a String, but a System.Management.Automation.Internal.ObjectReader, which make iex not working as expected... since this one is working correctly:

powershell -noprofile -noninteractive -command "$command = \"echo 12;\"; $command | iex"

Output:

12

2 Answers 2

6

The following would work:

Use a scriptblock:

echo "echo 12;" | powershell -noprofile -noninteractive -command { $input | iex }

Or use single quotes to avoid the string interpolation:

 echo "echo 12;" | powershell -noprofile -noninteractive -command '$input | iex'

so the $input variable isn't expanded, and the string '$input' is passed to iex.

Both of these give me "12".

Sign up to request clarification or add additional context in comments.

2 Comments

This is working when you're running this from another PowerShell, but it's not working when using it from cmd.exe. Why?
That's to do with the quotes around the "echo 12;". If you execute this in powershell echo "echo 12;" gives echo 12; without the quotes. In cmd.exe you'll see that it writes "echo 12;", which is then interpreted as a string by Powershell. To make it work in cmd.exe you can try: echo echo 12; | powershell -noprofile -noninteractive -command "$input | iex"
4

Just use - as a source file "name":

echo "echo 12;" | powershell -noprofile -noninteractive -

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.