Showing posts with label progress bar. Show all posts
Showing posts with label progress bar. Show all posts

Sunday, January 28, 2007

Inline Progress Bar

Here is a quick one to display inline precentage count (basic demo)

# sets the display two lines bellow the current cursor Y position

$CursorY = $host.UI.RawUI.CursorPosition.Y+2
write-host "`nprocessing something...`n" -fore yellow
1..100 | % {     
   [Console]::SetCursorPosition(0,$CursorY)     
   [Console]::Write("please wait... [ $_% ]")     
   sleep -m 50
}
write-host "`nProcessing something, is done.`n" -fore yellow

and for a "real-world" example:

$events = get-eventlog -logname system
$CursorY = $host.UI.RawUI.CursorPosition.Y+2
for($i -eq 0;$i -lt $events.count;$i++){     
   [int]$p=[math]::round(((100*$i)/$events.count))     
   [Console]::SetCursorPosition(0,$CursorY)     
   [Console]::Write("Percent complete: "+[String]::Format("{0:N0}{1}" ,$p,"%"))
}

write-host `n

$hay