PowerShell - Overwriting line written with Write-Host

You cannot overwrite a line in a Powershell window. What you can do is blank the window with cls(Clear-Host):

# loop code
cls
Write-Host "`rWriting $outputFileName ($i/$fileCount)... $perc%"
# end loop

But what you should really be using is Write-Progress, a cmdlet built specifically for this purpose:

# loop code
Write-Progress -Activity "Writing $outputFileName" -PercentComplete $perc
# end loop

More on Write-Progress here: http://technet.microsoft.com/en-us/library/hh849902.aspx


As a tweak to Raf's answer above, You don't have to wipe the screen every time to update your last line. Calling Write-Host with -NoNewLine and carriage return `r is enough.

for ($a=0; $a -le 100; $a++) {
  Write-Host -NoNewLine "`r$a% complete"
  Start-Sleep -Milliseconds 10
}
Write-Host #ends the line after loop

Tags:

Powershell