Timing a command's execution in PowerShell

前端 未结 7 828
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 01:13

Is there a simple way to time the execution of a command in PowerShell, like the \'time\' command in Linux?
I came up with this:

$s=Get-Date; .\\do_somet         


        
7条回答
  •  情歌与酒
    2020-11-28 01:59

    Yup.

    Measure-Command { .\do_something.ps1 }
    

    Note that one minor downside of Measure-Command is that you see no stdout output.

    [Update, thanks to @JasonMArcher] You can fix that by piping the command output to some commandlet that writes to the host, e.g. Out-Default so it becomes:

    Measure-Command { .\do_something.ps1 | Out-Default }
    

    Another way to see the output would be to use the .NET Stopwatch class like this:

    $sw = [Diagnostics.Stopwatch]::StartNew()
    .\do_something.ps1
    $sw.Stop()
    $sw.Elapsed
    

提交回复
热议问题