Skip to main content

Displays a progress bar within a PowerShell command window.

# -------------------------------------------------------------------
# To run a sequence of 4 steps and report each step's progress:
# -------------------------------------------------------------------

$numSteps = 4
$currentStep = 0
$progressParams = @{
    'Activity' = 'Some lengthy operation'
    'Id' = 1
}

$currentStep++
Write-Progress @progressParams -Status 'Step 1' -PercentComplete (($currentStep / $numSteps) * 100)
Write-Output 'Doing stuff in step 1...'
Start-Sleep -Seconds 1

$currentStep++
Write-Progress @progressParams -Status 'Step 2' -PercentComplete (($currentStep / $numSteps) * 100)
Write-Output 'Doing stuff in step 2...'
Start-Sleep -Seconds 1

$currentStep++
Write-Progress @progressParams -Status 'Step 3' -PercentComplete (($currentStep / $numSteps) * 100)
Write-Output 'Doing stuff in step 3...'
Start-Sleep -Seconds 1

$currentStep++
Write-Progress @progressParams -Status 'Step 4' -PercentComplete (($currentStep / $numSteps) * 100)
Write-Output 'Doing stuff in step 4...'
Start-Sleep -Seconds 1

Write-Output ('Finished {0}.' -f $progressParams.Activity.ToString())
Write-Progress @progressParams -Completed

# -------------------------------------------------------------------
# Display the progress of a for loop:
# -------------------------------------------------------------------

for ($i = 1; $i -le 100; $i++ )
{
    Write-Progress -Activity 'Search in Progress' -Status "$i% Complete:" -PercentComplete $i
    Start-Sleep -Milliseconds 50
}