0

I have a Windows executable which I run from powershell using:

& $command | Out-Default

The command can take several minutes to run and will periodically output messages to the console to show it's status. When I run this executable through powershell using the above style, I see the messages that the executables outputs shown in the powershell window. It looks ok, but I'd like to begin using Write-Progress to show the status of the executing command.

Is there any way to dynamically feed the output of this executable (as it runs) to Write-Progress so that it can show a progress bar with the message set to the last line of output from the executable?

2
  • 2
    Can you provide sample output? There's no way for Write-Progress to know how far along the exe is. As far as I know, you would need to parse the output to calculate some sort of percentage from it. Commented Dec 14, 2020 at 6:36
  • Thanks, but I think the accepted answer below covers what I need. I am not concerned with percentages or calculating progress. I just want to be able to pipe each line of the executables output to write-progress so that it keeps the caller up to date with what's happening (basically moving the output from stdout to write-progress). Accepted answer seems to do the trick! Commented Dec 14, 2020 at 23:17

1 Answer 1

1

Relaying standard output messages as progress status updates can be done by simply piping the output from the executable to ForEach-Object and passing it to Write-Progress:

function Invoke-WithProgress
{
  param([string]$Path)

  $Actvity = "Executing '$Path'..."

  # This one is optional
  Write-Progress -Activity $Actvity -Status "Starting out!"

  & $Path |%{
    # Provide each new line of output as a status update
    Write-Progress -Activity $Actvity -Status $_
  }

  # Complete the progress actvity
  Write-Progress -Activity $Actvity -Completed
}

Invoke-WithProgress $command
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.