次の方法で共有


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 ループの進行状況をそれぞれ対応する進行状況バーに表示します。

Write-Progress 2 番目の進行状況バーのコマンドには、最初の進行状況バーと区別する 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
}

このコマンドは、System イベント ログ内で "bios" という文字列を検索するコマンドの進行状況を表示します。

PercentComplete パラメーターの値は、処理されたイベントの数を取得$Events.countした$Iイベントの合計数で割り、その結果に 100 を乗算することによって計算されます。

パラメーター

-Activity

ステータス バーの上の見出しのテキストの 1 行目を指定します。 このテキストは、進行状況が表示されているアクティビティについて説明します。

Type:String
Position:1
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 を指定します。 1 つのコマンドで複数の進行状況バーを作成する場合は、このパラメーターを使用します。 進行状況バーに異なる ID を付けなかった場合、進行状況バーは、順に表示される代わりに重なり合って表示されます。

Type:Int32
Position:3
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

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

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Status

ステータス バーの上の見出しのテキストの 2 行目を指定します。 このテキストは、アクティビティの現在の状態を記述します。

Type:String
Position:2
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

入力

None

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

出力

None

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

メモ

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

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