Using Write-Progress in PowerShell – A Complete Guide
Welcome back to Wahmans PowerShell Blog! Today we are diving into a useful cmdlet that helps make long-running scripting operations friendlier and more informative for users: Write-Progress.
Microsoft describes Write-Progress as a way to display a progress bar within a PowerShell command window. This doesn’t just look nice—it can be incredibly helpful to let users know something is processing, how far along it is, and what’s next.
Why Use Write-Progress?
When you’re executing scripts that perform multiple steps or take time to finish, displaying progress helps to improve user experience and adds professionalism to your scripts. Let’s look at a few examples, starting simple and progressively getting advanced.
Example 1: Basic Loop Progress
This is a simple way to show progress through a `for` loop:
for ($i = 1; $i -le 100; $i++) {
Write-Progress -Activity "Processing" -Status "Step $i of 100" -PercentComplete $i
Start-Sleep -Milliseconds 50
}
This example simulates work by pausing for 50 milliseconds and updates the status bar as it loops from 1 to 100.
Example 2: Progress While Downloading Files
If you’re automating downloads, give your users visibility with a progress bar:
$files = @("file1.zip", "file2.zip", "file3.zip")
for ($i = 0; $i -lt $files.Count; $i++) {
$percent = ($i / $files.Count) * 100
Write-Progress -Activity "Downloading Files" -Status "Downloading $($files[$i])" -PercentComplete $percent
Start-Sleep -Seconds 2 # Simulate download
}
The progress bar shows which file is currently downloading and the overall progression.
Example 3: Nested Progress Bars
You can use Write-Progress with -Id and -ParentId to show hierarchical tasks:
for ($outer = 1; $outer -le 3; $outer++) {
Write-Progress -Id 1 -Activity "Main Task" -Status "Phase $outer of 3" -PercentComplete (($outer / 3) * 100)
for ($inner = 1; $inner -le 10; $inner++) {
Write-Progress -Id 2 -ParentId 1 -Activity "Sub Task" -Status "Item $inner of 10" -PercentComplete (($inner / 10) * 100)
Start-Sleep -Milliseconds 100
}
}
This establishes a parent-child relationship between two progress bars: one for the primary task and one for the sub-task.
Example 4: Real-Time File Scanning with Progress
Let’s scan all files in a directory and show progress as we go:
$files = Get-ChildItem -Recurse -File -Path "C:\Logs"
$total = $files.Count
for ($i = 0; $i -lt $total; $i++) {
$file = $files[$i]
$percent = ($i / $total) * 100
Write-Progress -Activity "Scanning Files" -Status "Processing $($file.Name)" -PercentComplete $percent
Start-Sleep -Milliseconds 50 # Simulate file work
}
This script recurses through C:\Logs and shows a progress bar while “processing” each file.
Wrapping Up
The Write-Progress cmdlet is a small feature that delivers big value, particularly in long-running scripts or user-facing automation tools. From simple loops to nested workflows, adding progress updates has never been easier.
Happy scripting, and I will see you in the next post!
Leave a Reply