Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

How to ignore pause - press any key - in Powershell while executing file

Using Powershell I am starting some executable using invoke-expression like:

Invoke-Expression "c:\exec.exe"

now my problem is this exec is showing something like "pause and press any key to continue".

I tried:

Function Run-Tool
{
    Invoke-Expression "c:\exec.exe"
    $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

But no luck.

My question is can I ignore that message some sort of suppression or is there a way to monitor output and simulate pressing any key?

Upvotes: 3

Views: 5381

Answers (3)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

If exec.exe is a regular console app expecting a newline a la:

Console.Write("Press any key to exit...");
Console.ReadLine();

Then you could just pipe a newline to it:

"`n" |& C:\exec.exe

Upvotes: 8

ClumsyPuffin
ClumsyPuffin

Reputation: 4059

you can redirect the enter key from powershell to the program by using processstartinfo and process :

$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "C:\yourexe.exe"; 
$psi.UseShellExecute = $false; 
$psi.RedirectStandardInput = $true;

$p = [System.Diagnostics.Process]::Start($psi);

Start-Sleep -s 2 # if your exe needs time to give output

$p.StandardInput.WriteLine("`n");

Upvotes: 2

Daniel Brixen
Daniel Brixen

Reputation: 1663

Using $host.UI.RawUI.ReadKey will not do what you are after - the ReadKey function waits for input and returns info on the key that was pressed. So instead of sending input, it is waiting for input.

This answer is related to your situation - perhaps you can use the method suggested there.

Upvotes: 0

Related Questions