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.

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,171 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,509 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

6 answers

Sort by: Most helpful
  1. MotoX80 31,816 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