0

I have the following command

Get-Content -Path out.log -Wait

which reads a file and waits.

I want to break the wait on any key press (or a specific key, doesn't matter).

Is it possible?

UPDATE

Ctrl + C is not an option because I cannot get rid of "Terminate batch job (Y/N)?" message.

abc123
  • 143
  • 1
  • 6

1 Answers1

1

The closest you can get is to spawn another PowerShell process and to kill that when you press a key. Or the user can simply close the new shell when done.

# Start Get-Content -Wait
# Get the Process ID. 
$command = 'get-content -path "C:\Users\Michael\Desktop\Vim\temp.txt" -Wait'

$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
$psProcess = Start-Process -FilePath pwsh -ArgumentList "-EncodedCommand",$encodedCommand -PassThru

# Wait for input. 
Write-Host "Press any key to to kill Get-Process -Wait."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null 

# Kill process.
$psProcess.Kill()

Failing that, there might be some other way to solve your problem.

What caused you to ask this question? Or what is the background for this problem?

You are only showing the Get-Content -Wait cmdlet. That alone does not generate Terminate batch job. What other code is running?

There are other tail or watch tools available. This search might be helpful. Or this thread. If you want access to Unix tools, it is possible to emulate them on Windows.