2

I try to run exe in a loop in PowerShell, but the program ends with Press any key to continue, so my loop hangs.

My code up to now is:

foreach ($num in 1..50) { .\somethig.exe }

In bash I could use yes command but I cannot find anything similar in PowerShell

1 Answer 1

1

Assuming you have to type in 'y' to get pass the pause,

PS> 1..3 | % { write-output y | cmd /c pause }
Press any key to continue . . .
Press any key to continue . . .
Press any key to continue . . .
PS> 

PS> 1..50 | % { write-output y | .\something.exe }

'%' is an alias for Foreach-Object

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

5 Comments

It doesn't work. I got error from application Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
Is your something.exe a Console or a Windows application?
Apparently it doesn't work with Console.ReadKey(). It does work with Console.ReadLine(). # 1..3 | % { write-output y | .\ConsoleApplication7.exe } Oh hai. Press a key. Key pressed. Exiting.
Console.Read() is also fine.
Your suggestion is incorrect. The behaviour you see is not the result of the write-output sending a y to the pause but the fact that connecting the cmd /c pause to a pipe disconnects the command from the console for input. The pause then does not wait but ends immediately irrespective of the piped input. You get the same result from 1..3 | % { $null | cmd /c pause }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.