PowerShell: Write-Progress

Problem

I wanted to get a better indication of my scripts progression.
This would definitely be an improvement for the scripts that take several minutes (or even hours) to finish.

Solution

After a few web searches I stumbled upon the Write-Progress cmdlet.

The Write-Progress cmdlet allows you to display your scripts progression in a neat little progress bar, while your script is running.

Example

This example script shows each item in a list.
The progress bar displays the current position relative to the total amount of items in the list.
The Sleep 1 is added so the progress bar is displayed long enough to be seen in this example.

$list = @("1","2","3")
Write-Progress -Activity "Working..." -PercentComplete 0 -CurrentOperation "0% complete" -Status "Please wait."
$total = $list.Count
$current = 0
foreach($item in $list){
	Write-Host $item
	Sleep 1
	$current++
	[int]$perc = ($current/$total)*100
	Write-Progress -Activity "Working..." -PercentComplete $perc -CurrentOperation "$perc% complete" -Status "Please wait."
}
Write-Progress -Activity "Working..." -Completed -Status "All done."

The progress bar looks like this:
progressBar

Advertisements

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: