powershellscripts.com

Tutorials  PowerShell Cmdlet Help for Write-Progress



NAME
Write-Progress

SYNOPSIS
Displays a progress bar within a Windows PowerShell command window.

SYNTAX
Write-Progress [-Activity] [-Status] [[-Id] ] [-Completed] [-CurrentOperation ] [-Pa
rentId ] [-PercentComplete ] [-SecondsRemaining ] [-SourceId ] []


DESCRIPTION
The Write-Progress cmdlet displays a progress bar in a Windows PowerShell command window that depicts the status of
a running command or script. You can select the indicators that the bar reflects and the text that appears above a
nd below the progress bar.


PARAMETERS
-Activity
Specifies the first line of text in the heading above the status bar. This text describes the activity whose pr
ogress is being reported.

Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Completed []
Indicates whether the progress bar is visible. If this parameter is omitted, Write-Progress displays progress i
nformation.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-CurrentOperation
Specifies the line of text below the progress bar. This text describes the operation that is currently taking p
lace.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Id
Specifies an ID that distinguishes each progress bar from the others. Use this parameter when you are creating
more than one progress bar in a single command. If the progress bars do not have different IDs, they are superi
mposed instead of being displayed in a series.

Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false

-ParentId
Identifies the parent activity of the current activity. Use the value -1 if the current activity has no parent
activity.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-PercentComplete
Specifies the percentage of the activity that is completed. Use the value -1 if the percentage complete is unkn
own or not applicable.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-SecondsRemaining
Specifies the projected number of seconds remaining until the activity is completed. Use the value -1 if the nu
mber of seconds remaining is unknown or not applicable.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-SourceId
Identifies the source of the record.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Status
Specifies the second line of text in the heading above the status bar. This text describes current state of the
activity.

Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false


This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer and OutVariable. For more information, type,
"get-help about_commonparameters".

INPUTS
None
You cannot pipe input to this cmdlet.


OUTPUTS
None
Write-Progress does not generate any output.


NOTES


If the progress bar does not appear, check the value of the $ProgressPreference variable. If the value is set t
o SilentlyContinue, the progress bar is not displayed. For more information about Windows PowerShell preference
s, see about_Preference_Variables.

The parameters of the cmdlet correspond to the properties of the ProgressRecord class (System.Management.Automa
tion.ProgressRecord). For more information, see the ProgressRecord topic in the Windows PowerShell Software Dev
elopment Kit (SDK).


-------------------------- EXAMPLE 1 --------------------------

C:\PS>for ($i = 1; $i -lt 101; $i++ )
{for ($j=0;$j -lt 10000;$j++) {} write-progress -activity "Search in Progress" -status "% Complete:" -percentcomple
te $i;}


Description
-----------
This command displays the progress of two nested For loops. The first loop counts to 100. For each increment of tha
t loop, the second loop counts to 10,000.
The Write-Progress command includes a status bar heading ("activity"), a status line, and the variable $i (the coun
ter in the For loop), which indicates the relative completeness of the task.





-------------------------- EXAMPLE 2 --------------------------

C:\PS>for($i = 1; $i -lt 101; $i++ ) {write-progress -activity Updating -status progress-> -percentcomplete $i -cur
rentOperation OuterLoop} for($i = 1; $i -lt 101; $i++ ) {write-progress -activity Updating -status progress -percen
tcomplete $i -id 1 -currentOperation InnerLoop}

Updating
progress ->
[oooooooooooooooooo ]

OutsideLoop

Updating
progress
[oooooooooooooooooo ]

InnerLoop


Description
-----------
This example displays the progress of two nested For loops, each of which is represented by a progress bar.

The Write-Progress command for the second progress bar includes the Id parameter that distinguishes it from the fir
st progress bar. Without the Id parameter, the progress bars would be superimposed on each other instead of being d
isplayed one below the other.





-------------------------- EXAMPLE 3 --------------------------

C:\PS>$events = get-eventlog -logname system

C:\PS> $events | foreach-object -begin {clear-host;$i=0;$out=""} `
-process {if($_.message -like "*bios*") {$out=$out + $_.Message};
$i = $i+1;`
write-progress -activity "Searching Events" `
-status "Progress:" -percentcomplete ($i/$events.count*100)} `
-end {$out}


Description
-----------
This command displays the progress of a command to find the string "bios" in the System event log.

In the first line of the command, the Get-EventLog cmdlet gets the events in the System log and stores them in the
$events variable.

In the second line, the events are piped to the ForEach-Object cmdlet. Before processing begins, the Clear-Host cmd
let is used to clear the screen, the $i counter variable is set to zero, and the $out output variable is set to the
empty string.


In the third line, which is the Process script block of the ForEach-Object cmdlet, the cmdlet searches the message
property of each incoming object for "bios". If the string is found, the message is added to $out.

In the fourth line, the $i counter variable is incremented to record that another event has been examined.

The fifth line uses the Write-Progress cmdlet with values for the Activity and Status text fields that create the f
irst and second lines of the progress bar heading, respectively. The PercentComplete parameter value is calculated
by dividing the number of events that have been processed ($i) by the total number of events retrieved ($events.cou
nt) and then multiplying that result by 100.

In the last line, the End parameter of the ForEach-Object cmdlet is used to display the messages that are stored in
the $out variable.






RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=113428
Write-Verbose
Write-Error
Write-Host
Write-Debug
Write-Output
Write-Warning