次の方法で共有


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 コマンドレットは、実行中のコマンドまたはスクリプトの状態を示す進行状況バーを 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 コマンドレットには、タスクの相対的な完全性を示すステータス バーの見出し 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

次の使用例は、入れ子になった 2 つの For ループの進行状況を表示します。それぞれのループは進行状況バーで表されます。

2 番目の進行状況バーの 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 で乗算することによって計算されます。

パラメーター

-Activity

ステータス バーの上にある見出しの最初の行のテキストを指定します。 このテキストでは、進行状況が報告されているアクティビティについて説明します。

型:String
配置:1
規定値:None
必須:True
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-Completed

進行状況バーが表示されるかどうかを示します。 このパラメーターを省略すると、Write-Progress 進行状況情報が表示されます。

型:SwitchParameter
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-CurrentOperation

進行状況バーの下のテキスト行を指定します。 このテキストでは、現在実行されている操作について説明します。

型:String
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-Id

各進行状況バーを他の進行状況バーと区別する ID を指定します。 1 つのコマンドで複数の進行状況バーを作成する場合は、このパラメーターを使用します。 進行状況バーに異なる ID がない場合は、系列に表示されるのではなく重ね合わされます。

型:Int32
配置:3
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-ParentId

現在のアクティビティの親アクティビティを指定します。 現在のアクティビティに親アクティビティがない場合は、-1 値を使用します。

型:Int32
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-PercentComplete

完了したアクティビティの割合を指定します。 達成率が不明または該当しない場合は、-1 値を使用します。

型:Int32
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-SecondsRemaining

アクティビティが完了するまでの残りの予測秒数を指定します。 残りの秒数が不明または該当しない場合は、-1 値を使用します。

型:Int32
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-SourceId

レコードのソースを指定します。

型:Int32
配置:Named
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

-Status

ステータス バーの上にある見出しの 2 行目のテキストを指定します。 このテキストでは、アクティビティの現在の状態について説明します。

型:String
配置:2
規定値:None
必須:False
パイプライン入力を受け取る:False
ワイルドカード文字を受け取る:False

入力

None

このコマンドレットに入力をパイプすることはできません。

出力

None

Write-Progress は出力を生成しません。

メモ

進行状況バーが表示されない場合は、$ProgressPreference 変数の値を確認します。 値が SilentlyContinue に設定されている場合、進行状況バーは表示されません。 Windows PowerShell の基本設定の詳細については、「about_Preference_Variables」を参照してください。

コマンドレットのパラメーターは、System.Management.Automation.ProgressRecord クラスのプロパティに対応します。 詳細については、MSDN ライブラリ ProgressRecord クラス を参照してください。