Taking on PowerShell one cmdlet at a time

Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will guide you through a PowerShell command each week, explaining when and how to use them. Adam will be covering Write-Progress this week.

When to use Write Progress
The Write-Progress cmdlet displays the progress bar in a PowerShell command windows that shows the status of a running script or command.
You can choose the indicators that the bar reflects, and the text that appears over and below the progress bar.

How to use Write Progress
Display the progress of nested For loops
for($I=1; $I-lt 101, $I++) Write–Progress–Activity updating–Status ‘Progress->’ PercentComplete$j CurrentOperationOuterLoop123456789for$I =1; $I-lt101; $I++) Write–Progress–Activity updating–Status ‘Progress->’ Percentcomplete $I–CurrentOperationOuterLoop)
This example shows the progress of two nested For loops. Each loop is represented by a progressbar.
The Write-Progress command to create the second progress bar has the Id parameter. This distinguishes it the first progress bar. (Write-Progress -Id 1)
The Id parameter would have prevented the progress bars from being displayed in a superimposed fashion instead of being displayed below each other.

Observe the progress when searching for a string
# Use Get-EventLog to get the events in the System log and store them in the $Events variable.$Events = Get-EventLog -LogName system# Pipe the events to the ForEach-Object cmdlet.$Events | ForEach-Object -Begin # In the Begin block, use Clear-Host to clear the screen. Clear-Host # Set the $i counter variable to zero. $i = 0 # Set the $out variable to a empty string. $out = “” -Process # In the Process script block search the message property of each incoming object for “install”. if($_.message -like “*install*”) # Append the matching message to the out variable. $out=$out + $_.Message # Increment the $i counter variable which is used to create the progress bar. $i = $i+1 # Use Write-Progress to output a progress bar. # The Activity and Status parameters create the first and second lines of the progress bar heading, respectively. Write-Progress -Activity “Searching Events” -Status “Progress:” -PercentComplete ($i/$Events.count*100) -End # Display the matching messages using the out variable. $out 123456789101112131415161718192021222324252627# Use Get-EventLog to get the events in the System log and store them in the $Events variable.$Events = Get-EventLog -LogName system# Pipe the events to the ForEach-Object cmdlet.$Events | ForEach-Object -Begin # In the Begin block, use Clear-Host to clear the screen. Clear-Host # Set the $i counter variable to zero. $i = 0 # Set the $out variable to a empty string. $out = “” -Process { # In the Process script block search the message property of each incoming object for “install”. if($_.message -like “*install*”) # Append the matching message to the out variable. $out=$out + $_.Message # Increment the $i counter variable which is used to create the progress bar. $i = $i+1

Posts created 191

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top