Redirect standard input (read-host) to a Powershell script

Question:

Here’s a sample powershell script:

Here’s a sample ‘test.txt’ file:

And we want to pass piped input to it from powershell. Here’s some I have tried:

even just trying to echo input doesn’t work either:

Every example above that doesn’t produce an error always prompts the user instead of accepting the piped input.

Note: My powershell version table says 4.0.-1.-1

Thanks

Edit/Result: To those looking for a solution, you cannot pipe input to a powershell script. You have to update your PS file. See the snippets below.

Answer:

The issue is that your script \.test.ps1 is not expecting the value.

Try this:


Alternatively, you can use the magic variable $input without a param part (I don’t fully understand this so can’t really recommend it):

Source:

Redirect standard input (read-host) to a Powershell script by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply