Write-Progress

在 PowerShell 命令視窗中顯示進度列。

Syntax

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

Description

Cmdlet 會在 Write-Progress 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、狀態行和變數 $i (迴圈中的 for 計數器),表示工作的相對完整性。

範例 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參數,進度列會彼此迭加,而不是顯示在另一個下方。

範例 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 參數值是透過除以擷取$Events.count的事件總數所處理$i的事件數目,然後將該結果乘以 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:False
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

指定標識碼,以區分每個進度列與其他進度列。 當您在單一命令中建立多個進度列時,請使用此參數。 如果進度列沒有不同的標識碼,則會加疊,而不是顯示在數列中。 不允許負值。

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 類別