Redirecting standard input\output in Windows PowerShell Redirecting standard input\output in Windows PowerShell powershell powershell

Redirecting standard input\output in Windows PowerShell


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

Get-Content input.txt | ./program > output.txt


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"


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