question

Ed7 avatar image
0 Votes"
Ed7 asked ObaidFarooqi-8403 edited

Measure the time a script in powershell will take to run

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-serverwindows-server-powershelloffice-scripts-excel-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Ed7
The tag openspecs-windows is dedicated to supporting issues related to open specifications. You can find open specifications at https://docs.microsoft.com/en-us/openspecs/. Your inquiry is not related to open specifications. I have removed the tag oepnspecs-windows from your post.

Regards,
Obaid Farooqi - MSFT

0 Votes 0 ·
StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered

Hi @Ed7,

you are actually using one of the recommended methods, maybe you are having some deltas in the data, hence the differences between the actual and measured execution time. Still there are two other way to check if the measurement you do is correct, Adma the Automator lists those here:

3 ways to measure your Powershell script's speed
https://www.pluralsight.com/blog/tutorials/measure-powershell-scripts-speed

I hope I could help ypou out. Regards,


If my reply was helpful please don't forget to upvote and/or accept as answer, thank you!
Regards,
Stoyan




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ed7 avatar image
0 Votes"
Ed7 answered Ed7 edited

Hi @StoyanChalakov ,

Thank you for your response.

When I run the script this is what I get and its estimated time:

155580-image.png


Then when it completes it does tell me how long it took

155519-image.png



Not to mention that with this command it will use the full RAM available

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


So something is not right



image.png (40.7 KiB)
image.png (125.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered

Hi,

Could it be be, because of the "&" in the name of the script? Can you please remove it and try again?
Did you also test the other options?

Regards,
Stoyan

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ed7 avatar image
0 Votes"
Ed7 answered

HI,

Yes I have removed the "&" and it takes longer than usual to run the script. and still uses a lot of resources and takes a lot of time

155620-image.png





I wonder if its because I am measureing the script I am running or not. But yesterday when I tried it did not behave as it is today


image.png (287.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ed7 avatar image
0 Votes"
Ed7 answered

Well it seems that is not giving me the error but the estimate time is still not right. I changed the cmmand for Measure-Command {"C:\Pathtoscript\Myscript.ps1"}

Need to understant why is giving the wrong time.

155691-image.png


And it took me

155627-image.png




image.png (13.0 KiB)
image.png (6.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 edited

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  
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.