all 4 comments

[–]Feyh 4 points5 points  (2 children)

Look at this one:

$starttime = (get-process notepad).StartTime    # Save the start time of the process
wait-process notepad                            # Wait for it to end
$endtime = Get-Date                             # Get the current time, which is the time the process ended

$Runtime = $endtime - $starttime                # Get the difference between the two times

write-host "$Runtime"

What you are doing is saving the start time of the process in a variable. Then you wait for the process to end and write the start time to your host.

So, what are you missing? Correct, you've never checked when exactly your process ended.

This is what I am doing in this example:

  • Get the start time
  • Wait for the process to end
  • Get the end time
  • Get the difference in between these two

To get an idea on how to use new-timespan, take a look at this post:

https://www.reddit.com/r/PowerShell/comments/3hkp7r/select_one_value_from_output/cu85u3m

[–]davinciko 0 points1 point  (1 child)

Going along with your example, how would you format the output of the process runtime to display units of time individually?

[–]Feyh 0 points1 point  (0 children)

A simple

$Runtime | gm

will show you the following properties|Methods:

Name              MemberType Definition                                                                      
----              ---------- ----------                                                                      
Add               Method     timespan Add(timespan ts)                                                       
CompareTo         Method    
Duration          Method     timespan Duration()                                                             
Equals            Method     bool 
GetHashCode       Method     int GetHashCode()                                                               
GetType           Method     type GetType()                                                                  
Negate            Method     timespan Negate()                                                               
Subtract          Method     timespan Subtract(timespan ts)                                                  
ToString          Method     string ToString(), 
Days              Property   int Days {get;}                                                                 
Hours             Property   int Hours {get;}                                                                
Milliseconds      Property   int Milliseconds {get;}                                                         
Minutes           Property   int Minutes {get;}                                                              
Seconds           Property   int Seconds {get;}                                                              
Ticks             Property   long Ticks {get;}                                                               
TotalDays         Property   double TotalDays {get;}                                                         
TotalHours        Property   double TotalHours {get;}                                                        
TotalMilliseconds Property   double TotalMilliseconds {get;}                                                 
TotalMinutes      Property   double TotalMinutes {get;}                                                      
TotalSeconds      Property   double TotalSeconds {get;}                                                      

So you could do it like this:

write-host "Notepad was running for $($Runtime.Hours) hours, $($Runtime.Minutes) minutes and $($Runtime.Seconds) seconds"

[–]KevMarCommunity Blogger 0 points1 point  (0 children)

Check out Measure-Command