共用方式為


Measure-Command

測量執行指令碼區塊和 Cmdlet 所需的時間。

Syntax

Measure-Command
       [-InputObject <PSObject>]
       [-Expression] <ScriptBlock>
       [<CommonParameters>]

Description

Cmdlet 會在 Measure-Command 內部執行腳本區塊或 Cmdlet、執行作業的時間,並傳回運行時間。

注意

腳本區塊會執行於 Measure-Command 目前範圍中,而不是子範圍。

範例

範例 1:測量命令

此範例會測量執行Get-EventLog命令所需的時間,以取得 Windows PowerShell 事件記錄檔中的事件。

Measure-Command { Get-EventLog "windows powershell" }

範例 2:比較來自 Measure-Command 的兩個輸出

第一個命令會測量處理遞歸 Get-ChildItem 命令所需的時間,該命令會使用 Path 參數只 .txt 取得目錄中的檔案 C:\Windows 及其子目錄。

第二個命令會測量處理使用提供者特定 ' 參數的 Get-ChildItem 遞歸命令所花費的時間。

這些命令會顯示在PowerShell命令中使用提供者特定篩選的值。

Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 8
Milliseconds      : 618
Ticks             : 86182763
TotalDays         : 9.9748568287037E-05
TotalHours        : 0.00239396563888889
TotalMinutes      : 0.143637938333333
TotalSeconds      : 8.6182763
TotalMilliseconds : 8618.2763

Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 140
Ticks             : 11409189
TotalDays         : 1.32050798611111E-05
TotalHours        : 0.000316921916666667
TotalMinutes      : 0.019015315
TotalSeconds      : 1.1409189
TotalMilliseconds : 1140.9189

範例 3:將管線輸入傳送至 Measure-Command

透過管線傳送至 Measure-Command 的物件可供傳遞至 Expression 參數的腳本區塊使用。 腳本區塊會針對管線上的每個物件執行一次。

# Perform a simple operation to demonstrate the InputObject parameter
# Note that no output is displayed.
10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_; $i++) {$i} }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 12
Ticks             : 122672
TotalDays         : 1.41981481481481E-07
TotalHours        : 3.40755555555556E-06
TotalMinutes      : 0.000204453333333333
TotalSeconds      : 0.0122672
TotalMilliseconds : 12.2672

範例 4:顯示已測量命令的輸出

若要在 中Measure-Command顯示表示式的輸出,您可以使用管線將 。Out-Default

# Perform the same operation as above adding Out-Default to every execution.
# This will show that the ScriptBlock is in fact executing for every item.
10, 20, 50 | Measure-Command -Expression {for ($i=0; $i -lt $_; $i++) {$i}; "$($_)" | Out-Default }

10
20
50


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 11
Ticks             : 113745
TotalDays         : 1.31649305555556E-07
TotalHours        : 3.15958333333333E-06
TotalMinutes      : 0.000189575
TotalSeconds      : 0.0113745
TotalMilliseconds : 11.3745

範例 5:測量子範圍中的執行

Measure-Command 會執行目前範圍內的腳本區塊,因此腳本區塊可以修改目前範圍中的值。 若要避免變更目前的範圍,您必須將腳本區塊包裝在大括弧 () {} ,並使用調用運算符 (&) 在子範圍中執行區塊。

$foo = 'Value 1'
$null = Measure-Command { $foo = 'Value 2' }
$foo
$null = Measure-Command { & { $foo = 'Value 3' } }
$foo

Value 2
Value 2

如需調用運算符的詳細資訊,請參閱 about_Operators

參數

-Expression

指定正在計時的運算式。 將表達式括在大括弧 ({}) 。

Type:ScriptBlock
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-InputObject

系結至 InputObject 參數的對像是傳遞至 Expression 參數之腳本區塊的選擇性輸入。 在腳本區塊內, $_ 可用來參考管線中的目前物件。

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

輸入

PSObject

您可以使用管線會傳送至 Measure-Command

輸出

TimeSpan

Measure-Command 會傳回代表結果的時間範圍物件。