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 命令窗口中显示一个进度栏,用于描述正在运行的命令或脚本的状态。 你可以选择进度栏反映的指示器,以及显示在进度栏上方和下方的文本。

PowerShell 7.2 添加了 $PSStyle 自动变量,用于控制 PowerShell 如何使用 ANSI 转义序列显示某些信息。 成员 $PSStyle.Progress 允许你控制进度视图栏呈现。

  • $PSStyle.Progress.Style - 设置呈现样式的 ANSI 字符串。
  • $PSStyle.Progress.MaxWidth - 设置视图的最大宽度。 默认为 120。 最小值为 18。
  • $PSStyle.Progress.View - 具有 MinimalClassic 值的枚举。 Classic 为现有呈现,无更改。 Minimal 为单行最小呈现。 Minimal 为默认值。

有关 的详细信息 $PSStyle,请参阅 about_ANSI_Terminals.md

注意

如果主机不支持虚拟终端,$PSStyle.Progress.View 会自动设置为 Classic

示例

示例 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

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

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

$PSStyle.Progress.View = 'Classic'

for($I = 0; $I -lt 10; $I++ ) {
    $OuterLoopProgressParameters = @{
        Activity         = 'Updating'
        Status           = 'Progress->'
        PercentComplete  = $I * 10
        CurrentOperation = 'OuterLoop'
    }
    Write-Progress @OuterLoopProgressParameters
    for($j = 1; $j -lt 101; $j++ ) {
        $InnerLoopProgressParameters = @{
            ID               = 1
            Activity         = 'Updating'
            Status           = 'Progress'
            PercentComplete  = $j
            CurrentOperation = 'InnerLoop'
        }
        Write-Progress @InnerLoopProgressParameters
        Start-Sleep -Milliseconds 25
    }
}

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

此示例将进度视图设置为 Classic ,然后显示两个嵌套 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 an 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
    # Determine the completion percentage
    $Completed = ($i/$Events.count) * 100
    # 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 $Completed
} -End {
    # Display the matching messages using the out variable.
    $out
}

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

PercentComplete 参数值的计算方法是将已处理$i的事件数除以检索$Events.count到的事件总数,然后将该结果乘以 100。

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

$PSStyle.Progress.View = 'Classic'

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 -Milliseconds 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

此 cmdlet 不返回任何输出。

备注

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

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