Powershell

PowerShell Progress Bar

Some commands or operations take time to execute and until execution they do not show any results. Such a situation can create a thinking point in the user’s head that either the command is functioning or hung up. To overcome this problem, a progress bar can be used to get the status of the ongoing command.

This article provides a detailed guide to get a progress bar on your PowerShell commands. PowerShell supports several cmdlets and functions to perform any task. In the case of the progress bar, the Write-progress cmdlet of PowerShell is exercised to attain the progress status of the commands. You would learn the basic understandings and work of the Write-Progress cmdlet and its supported parameters.

How PowerShell Progress bar can be obtained

As mentioned earlier, the PowerShell progress bar can be obtained by using the Write-Progress cmdlet. The Write-Progress command of PowerShell functions on the syntax provided below:

> Write-Progress <Parameters>

The above syntax is dependent on the parametric support and the parameters supported by Write-Progress commands are defined as follows:

  • Activity: This parameter permits to show a line above/before the progress bar.
  • Completed: Indicates the visibility of the progress bar.
  • CurrentOperation: This shows the line after/below the progress bar.
  • Status: The values passed in this parameter are displayed on the progress bar.
  • Id: Used to identify the progress bars and this option is used where multiple progress bars are being created.
  • ParentId: Denotes a parent activity of the currently ongoing activity.
  • PercentComplete: Presents the percentage value that shows how many percent of activity has been completed.
  • SourceId: This parameter specifies the source of the record and this parameter cannot be used with ParentId because both lead to the same meaning.

How to get PowerShell Progress bar

The PowerShell progress bar functions on the grounds supported by Write-Progress. This section provides examples that provide usage of Write-Progress.

Example 1 : Progress bar with foreach loop
Let’s say we want to get the progress bar on the Show-Process cmdlet. For that, we are creating a PowerShell script where we will write several commands that would help in doing so.

For scripting mode, open the PowerShell ISE as an administrator by searching it from the search bar of Windows:

We have created a progress bar that shows the process that has a Handles value greater than or equal to 1000.

The script is named progress.ps1 and contains the following lines:

The first line filters the processes that have Handles greater than or equal to 1000 and the value is stored in a variable named $proc.

  • Second-line counts the $proc and stores in a variable $total_count (it will help in getting percentage)
  • A variable ($i) is initialized
  • Foreach loop started with looking into each process ($p) in the $proc variable
  • The loop is incrementing by 1 after each iteration
  • $percentage calculates the percentage of processes after each iteration.
  • The Write-Progress cmdlet is applied with -Activity and -Status parameters
  • The Start-Sleep cmdlet is used to get each iteration after 200 milliseconds.
$proc=Get-Process | Where-Object Handles -GE 1000  
$total_proc=$proc.Count
$i=0
Foreach($p in $proc){
$i=$i+1
$percentage=($i/$total_proc)*100
Write-Progress -Activity $p -Status "$percentage %" -PercentComplete $percentage
Start-Sleep -Milliseconds 200
}

To run the script, open PowerShell and navigate to the directory where your script is saved. We have executed the progress.ps1 script placed in the C:\scripts directory and the output is shown below.

> .\progress.ps1

Example 2: Getting the progress bar using For loop
In this example. For loop is taken as a demonstrative pick to show the progress. For assistance, the script for.ps1 is used that contains the execution of for loop with Write-Progress cmdlet:

  • The variable $i is initialized at 100: <$i = 100>
  • In each iteration, the value is decreased by “1“: <$i —>
  • The loop runs until the value is greater than 10: <$i -gt 10>
  • In the body of the For loop, Write-Progress is applied along with Activity, Status, and PercentComplete parameters. Moreover, the Start-Sleep cmdlet in the code is to execute each iteration with a gap of 150 milliseconds for a better overview of the progress bar.
for ($i = 100; $i -gt 10; $i-- )
{
    Write-Progress -Activity "Descending" -Status "$i%" -PercentComplete $i
    Start-Sleep -Milliseconds 150
}

For the execution of a script, navigate your terminal to the location where the script is saved. We have executed the for.ps1 as seen in the image below:

> .\for.ps1

Example 3: Get progress bar using While loops
In this example, the While loop is used here to get the progress bar in PowerShell. A new PowerShell script is created with a name while.ps1 that contains the following lines of code with the description provided below:

  • The first line stores the processes that have CPU(s) greater than or equal to 4:
  • The second line counts the processes that are extracted in the first line  
  • The $i variable is initialized at 0:
  • While loop is started and the condition is set to $i -lt 100:
  • The Activity and Status parameters are set to Descending and $i respectively:
  • The Start-Sleep cmdlet executes each iteration with a gap of 150 milliseconds:
  • The last line of the code increments the $i by 1:
$processes=Get-Process | Where-Object CPU -GE 4  
$total=$processes.Count
$i = 0
while ($i -lt 100)
{
  Write-Progress -Activity "Ascending" -Status "$i%" -PercentComplete $i
  Start-Sleep -Milliseconds 150
  $i++
}

After executing the script, you would observe the progress bar as shown in the image below:

> .\while.ps1

Conclusion

The progress bar functionality is practiced by getting the progress status of any task. It is quite helpful to look for the status of any task as there may exist commands that take time for execution. To get a progress bar, PowerShell supports the Write-Progress cmdlet. This article demonstrates the way to add a progress bar using the Write-Progress cmdlet. The functionality of the Write-Progress cmdlet can be extended by using the set of parameters supported by Write-Progress. Additionally, this guide provides several examples that demonstrate the use of Write-Progress in various scenarios.

About the author

Adnan Shabbir