So this one might be pretty simple you say, just use measure-command right? But what if I already ran the command and didn’t time it? What if it took really long time and running it again isn’t an option?
This is when built-in history helps! If you run
Get-History | Get-Member
You will see the following:
TypeName: Microsoft.PowerShell.Commands.HistoryInfo Name MemberType Definition ---- ---------- ---------- Clone Method Microsoft.PowerShell.Commands.HistoryInfo Clone() Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() CommandLine Property System.String CommandLine {get;} EndExecutionTime Property System.DateTime EndExecutionTime {get;} ExecutionStatus Property System.Management.Automation.Runspaces.PipelineState ExecutionStatus {get;} Id Property System.Int64 Id {get;} StartExecutionTime Property System.DateTime StartExecutionTime {get;}
Notice the properties “StartExecutionTime” and “EndExecutionTime”. For every command kept in PowerShell history, it keeps track of when it started and when it ended. To see how long it took to run the command, you can just use built-in math functionality. Here is how:
$h = get-history $h[-1].EndExecutionTime - $h[-1].StartExecutionTime
The output would look something like this:
Days : 0 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 46 Ticks : 467997 TotalDays : 5.41663194444444E-07 TotalHours : 1.29999166666667E-05 TotalMinutes : 0.000779995 TotalSeconds : 0.0467997 TotalMilliseconds : 46.7997
This also applies to your scripts. If you ran script, reported time will be how long script took to execute. It does not keep track of individual commands contained within your script but you already knew that, didn’t you?
Originally posted at http://blogs.technet.com/bshukla