Measure the time a script in powershell will take to run

Ed7 96 Reputation points
2021-12-07T10:05:43.833+00:00

Hello,

Could you tell me how can I check the estimated time a a script will take to run?
I have used this so far but it seems failing giving me a wrong estimated time.
After running a script it toke less time than the estimate time

Measure-Command -Expression{
powershell.exe "C:\Pathtoscript\Myscript.ps1"}

Thank you in advance.

Microsoft 365 and Office Development Other
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
{count} votes

6 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-12-07T14:48:56.02+00:00

    The image where you show the task manager is for the ISE process. But in your script you are launching a separate Powershell.exe to run your Generatinghash script. If you were doing a lot of testing then the ISE memory usage will include all of the variable and data that you've been playing with. Close ISE and reopen it

    When you run it like this... Measure-Command {"C:\Pathtoscript\Myscript.ps1"} Then that script will execute in the memory space of the Powershell.exe process that running the calling script.

    but the estimate time is still not right

    What's not right about it? For your 2 images in your last post, we have no context. What are you running?

    Add code to the Generatinghash script to report on execution time.

    $StartTime = get-date 
    start-sleep -Seconds 5 
    $RunTime = New-TimeSpan -Start $StartTime -End (get-date) 
    "Execution time was {0} hours, {1} minutes, {2} seconds and {3} milliseconds." -f $RunTime.Hours,  $RunTime.Minutes,  $RunTime.Seconds,  $RunTime.Milliseconds  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.