共用方式為


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 命令視窗中顯示進度列,描述執行中命令或腳本的狀態。 您可以選擇進度列反映的指示器及進度列上下方顯示的文字。

範例

範例 1:顯示 For 循環的進度

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

此命令會顯示 For 迴圈從 1 計算至 100 的進度。

Cmdlet Write-Progress 包含狀態列標題 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-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 "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 參數值的計算方式是將已處理$I的事件數目除以擷取$Events.count的事件總數,然後將該結果乘以 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

指定區別每個進度列的識別碼。 在單一命令中建立超過一個進度列時,請使用此參數。 如果進度列沒有不同的識別碼,它們會重疊,而不會以序列方式顯示。

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