共用方式為


Write-Progress

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

語法

Default (預設值)

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

Description

Write-Progress Cmdlet 會在 Windows PowerShell 命令視窗中顯示進度列,描述執行中命令或腳本的狀態。 您可以選取列所反映的指標,以及進度列上方和下方顯示的文字。

範例

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

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

此命令會顯示計算從 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 命令包含 標識子 參數,可區分它與第一個進度列。

如果沒有 標識碼 參數,進度列會彼此迭加,而不是顯示在另一個下方。

範例 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。

參數

-Activity

指定狀態列上方標題中的第一行文字。 此文字描述報告進度的活動。

參數屬性

類型:String
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:1
必要:True
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Completed

指出進度列是否可見。 如果省略此參數,Write-Progress 會顯示進度資訊。

參數屬性

類型:SwitchParameter
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-CurrentOperation

指定進度列下方的文字行。 此文字描述目前正在進行的作業。

參數屬性

類型:String
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Id

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

參數屬性

類型:Int32
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:3
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-ParentId

指定目前活動的父活動。 如果目前活動沒有父活動,請使用值 -1。

參數屬性

類型:Int32
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-PercentComplete

指定已完成的活動百分比。 如果百分比完成未知或不適用,請使用值 -1。

參數屬性

類型:Int32
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-SecondsRemaining

指定活動完成前的預計秒數。 如果剩餘的秒數未知或不適用,請使用值 -1。

參數屬性

類型:Int32
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-SourceId

指定記錄的來源。

參數屬性

類型:Int32
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Status

指定狀態列上方標題中的第二行文字。 此文字描述活動的目前狀態。

參數屬性

類型:String
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:2
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

CommonParameters

此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters

輸入

None

您無法使用管線將輸入傳送至此 Cmdlet。

輸出

None

Write-Progress 不會產生任何輸出。

備註

如果進度列未出現,請檢查 $ProgressPreference 變數的值。 如果此值設定為 SilentlyContinue,則不會顯示進度列。 如需 Windows PowerShell 喜好設定的詳細資訊,請參閱 about_Preference_Variables

Cmdlet 的參數會對應至 System.Management.Automation.ProgressRecord 類別的屬性。 如需詳細資訊,請參閱 MSDN 連結庫中 ProgressRecord 類別