Write-Progress

在 PowerShell 命令窗口中显示一个进度栏。

语法

Write-Progress
     [-Activity] <String>
     [[-Status] <String>]
     [[-Id] <Int32>]
     [-PercentComplete <Int32>]
     [-SecondsRemaining <Int32>]
     [-CurrentOperation <String>]
     [-ParentId <Int32>]
     [-Completed]
     [-SourceId <Int32>]
     [<CommonParameters>]

说明

Write-Progress cmdlet 在 PowerShell 命令窗口中显示进度栏,描述正在运行的命令或脚本的状态。 你可以选择进度栏反映的指示器,以及显示在进度栏上方和下方的文本。

示例

示例 1:显示 For 循环的进度

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

此命令将显示一个从 1 数到 100 的 For 循环的进度。

Write-Progress该 cmdlet 包括状态栏标题Activity、状态行和变量$i (For 循环) 中的计数器,该计数器指示任务的相对完整性。

示例 2:显示嵌套 For 循环的进度

for($I = 1; $I -lt 101; $I++ )
{
    Write-Progress -Activity Updating -Status 'Progress->' -PercentComplete $I -CurrentOperation OuterLoop
    for($j = 1; $j -lt 101; $j++ )
    {
        Write-Progress -Id 1 -Activity Updating -Status 'Progress' -PercentComplete $j -CurrentOperation InnerLoop
    }
}

Updating
Progress ->
 [ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo]
OuterLoop
Updating
Progress
 [oooooooooooooooooo                                                   ]
InnerLoop

本示例显示两个嵌套的 For 循环的进度,每个循环由一个进度栏表示。

Write-Progress第二个进度栏的命令包括 ID 参数,该参数将其与第一个进度栏区区分开来。

如果没有 ID 参数,进度栏将相互叠加,而不是显示在另一个下方。

示例 3:搜索字符串时显示进度

# Use Get-WinEvent to get the events in the System log and store them in the $Events variable.
$Events = Get-WinEvent -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 "bios".
    if($_.message -like "*bios*")
    {
        # 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
}

此命令将显示一个用于在系统事件日志中查找字符串“bios”的命令的进度。

PercentComplete 参数值通过除以检索$Events.count的事件总数所处理$I的事件数,然后将结果乘以 100 来计算。

示例 4:显示嵌套进程每个级别的进度

foreach ( $i in 1..10 ) {
  Write-Progress -Id 0 "Step $i"
  foreach ( $j in 1..10 ) {
    Write-Progress -Id 1 -ParentId 0 "Step $i - Substep $j"
    foreach ( $k in 1..10 ) {
      Write-Progress -Id 2  -ParentId 1 "Step $i - Substep $j - iteration $k"; start-sleep -m 150
    }
  }
}

Step 1
     Processing
    Step 1 - Substep 2
         Processing
        Step 1 - Substep 2 - Iteration 3
             Processing

在此示例中,可以使用 ParentId 参数具有缩进的输出,以在每个步骤的进度中显示父/子关系。

参数

-Activity

指定状态栏上方标题中的第一行文本。 此文本描述要报告进度的活动。

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Completed

指示进度栏是否可见。 如果省略此参数, Write-Progress 则显示进度信息。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-CurrentOperation

指定进度栏下方的文本行。 此文本描述当前正在进行的操作。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Id

指定用于区分各个进度栏的 ID。 在单个命令中创建多个进度栏时,请使用此参数。 如果这些进度栏不具有不同的 ID,则它们会重叠起来,而不是连续地显示。 不允许使用负值。

Type:Int32
Position:2
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ParentId

指定当前活动的父活动。 如果当前活动没有父活动,请使用值 -1。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-PercentComplete

指定已完成活动的百分比。 如果完成百分比未知或不适用,请使用值 -1。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-SecondsRemaining

指定预计的在完成活动之前剩余的秒数。 如果剩余秒数未知或不适用,请使用值 -1。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-SourceId

指定记录的源。 可以使用它代替 ID ,但不能与其他参数(如 ParentId)一起使用。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Status

指定状态栏上方标题中的第二行文本。 此文本描述活动的当前状态。

Type:String
Position:1
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

None

不能通过管道将输入传递给此 cmdlet。

输出

None

Write-Progress 不生成任何输出。

备注

如果未显示进度栏,请检查变量的值 $ProgressPreference 。 如果将该值设置为 SilentlyContinue,则不会显示进度栏。 有关 PowerShell 首选项的详细信息,请参阅 about_Preference_Variables

cmdlet 的参数对应于 System.Management.Automation.ProgressRecord 类的属性。 有关详细信息,请参阅 ProgressRecord 类