PowerShell:在Start-Process -Wait执行时同步显示Write-Progress进度条
Hey there! The issue you're running into makes total sense—right now, your Start-Process -Wait is blocking PowerShell from running the progress bar code until the installer finishes entirely. To get them running side by side, we need to launch the installer in the background, monitor its status, and update the progress bar simultaneously.
Here's a working solution:
Since most silent installers don't expose real-time progress data, we'll create a simulated progress bar that runs until the installer process exits. You can tweak the timing to match your typical installation duration.
$localfolder = (Get-Location).Path # Launch the installer and capture its process object (no -Wait here!) $installerProcess = Start-Process -FilePath "$localfolder\Installer.exe" -ArgumentList "/silent /accepteula" -PassThru # Initialize progress tracking variables $progress = 0 $updateDelay = 200 # Adjust this to make the progress bar faster/slower (in milliseconds) # Loop while the installer is still running while (!$installerProcess.HasExited) { # Increment progress until we hit 100% if ($progress -lt 100) { $progress += 1 } # Update the progress bar Write-Progress -Activity "Installing Application" ` -PercentComplete $progress ` -Status "Progress: $progress%" ` -CurrentOperation "Installer running in background" # Pause briefly before next update Start-Sleep -Milliseconds $updateDelay } # Finalize the progress bar to show completion Write-Progress -Activity "Installing Application" ` -PercentComplete 100 ` -Status "Installation Complete!" ` -CurrentOperation "Done" # Give the user a moment to see the completed status Start-Sleep -Milliseconds 1000 # Clear the progress bar from the console Write-Progress -Activity "Installing Application" -Completed
How this works:
-PassThruParameter: Instead of using-Wait, we use-PassThruto get a reference to the installer process. This lets us check if it's still running without blocking the script.- While Loop: The loop continues as long as the installer process is active. Inside the loop, we increment the progress percentage and update the progress bar.
- Simulated Progress: Since we can't get real-time data from most silent installers, this creates a smooth "fake" progress bar. Adjust
$updateDelayto match how long your installer usually takes—smaller values make the bar fill faster, larger values slower. - Cleanup: After the installer exits, we set the progress to 100%, show a completion message, and then clear the progress bar from the console.
Bonus: If you need real progress (advanced)
If your installer writes progress updates to a log file, you could extend this script to parse the log and update the progress bar with real values. For example, if the log lines include percentages, you could read the log periodically and extract that number to feed into Write-Progress. But for most simple silent installs, the simulated progress bar is more than sufficient.
内容的提问来源于stack exchange,提问作者Chris

