102

What is the required syntax to redirect standard input/output on Windows PowerShell?

On Unix, we use:

$./program <input.txt >output.txt

How do I execute the same task in PowerShell?

1

6 Answers 6

122

You can't hook a file directly to stdin, but you can still access stdin.

Get-Content input.txt | ./program > output.txt
9
  • 12
    This reads the entire input.txt file into memory. Be careful if input.txt might be large. Nov 8, 2017 at 5:34
  • 4
    Which cat? In PowerShell, "cat" is an alias of Get-Content, so they are exactly the same thing. May 29, 2019 at 17:36
  • 13
    @JackO'Connor: No, Get-Content sends the lines it reads one by one through the pipeline.
    – mklement0
    Nov 25, 2019 at 23:59
  • 1
    It has been that way since PS v1. Jan 4, 2020 at 1:01
  • 11
    Wow, what an awkward way to do it... If the < operator is not currently in use, why not use it for what most people expect it to do =/
    – Jet Blue
    May 30, 2020 at 3:33
31

If there is someone looking for 'Get-Content' alternative for large files (as me) you can use CMD in PowerShell:

cmd.exe /c ".\program < .\input.txt"

Or you can use this PowerShell command:

Start-Process .\program.exe -RedirectStandardInput .\input.txt -NoNewWindow -Wait

It will run the program synchronously in same window. But I was not able to find out how to write result from this command to a variable when I run it in PowerShell script because it always writes data to the console.

EDIT:

To get output from Start-Process you can use option

-RedirectStandardOutput

for redirecting output to file and then read it from file:

Start-Process ".\program.exe" -RedirectStandardInput ".\input.txt" -RedirectStandardOutput ".\temp.txt" -NoNewWindow -Wait
$Result = Get-Content ".\temp.txt"
1
  • 1
    Thanks a lot!. This cmd command allowed me to redirect file content without new line that Powershell was adding to pipeline. Sep 8 at 9:44
10

For output redirection you can use:

  command >  filename      Redirect command output to a file (overwrite)

  command >> filename      APPEND into a file

  command 2> filename      Redirect Errors 

Input redirection works in a different way. For example see this Cmdlet http://technet.microsoft.com/en-us/library/ee176843.aspx

5

Or you can do:

something like:

$proc = Start-Process "my.exe" "exe commandline arguments" -PassThru -wait -NoNewWindow -RedirectStandardError "path to error file" -redirectstandardinput "path to a file from where input comes"

if you want to know if process errored out, add following code:

$exitCode = $proc.get_ExitCode()

if ($exitCode){
    $errItem = Get-Item "path to error file"
    if ($errItem.length -gt 0){
        $errors = Get-Content "path to error file" | Out-String
    }
}

I find that this way I do have a better handle on execution of your scripts, when you need to handle external program/process. Otherwise I have encountered situations where script would hang out on some of external process errors.

1

You can also do this to have standard error and standard out go to the same place (note that in cmd, 2>&1 must be last):

get-childitem foo 2>&1 >log

Note that ">" is the same as "| out-file", and by default the encoding is unicode or utf 16. Also be careful with ">>", because it can mix ascii and unicode in the same text file. "| add-content" probably works better than ">>". "| set-content" might be preferable to ">".

There's 6 streams now. More info: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-5.1

I think all you can do is save to a text file and then read it into a variable after.

0

I am using PowerShell v7.3.x and Get-Content has been aliased to cat. To check for your PS do Get-Command cat

Since cat feels more like a shell thing, I do a cat content then | (pipe) then the exe to which we want to redirect input to.

Either use absolute paths or invoke from within dir

PS C:\user\working-dir > cat .\input | .\program.exe 

Attached screen snip Screen snip

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.