3

I have a script which copies some files to a backup storage. And I added a timer to see how much time it would use doing this. I used

$script:start = Get-Date
$start = (Get-Date)
Copy-Item $var0, $var1, $var2 Folders -force -recurse -verbose -Exclude name
$endFulltime = (Get-Date)
"$(($endFulltime-$startFulltime).totalminutes)"

The problem with this is the output. It gives me something like 0,34255234561245 minutes or if I put it in seconds, it just looks the same, but with different numbers. So the problem isn't that it gives me a wrong output; it just looks a bit messy.

Would it be possible to get output like 5 minutes and 43 seconds? Instead of this really long number.

4
  • Is there any reason you define "start" variable twice? Commented Jul 23, 2013 at 16:17
  • I was answering the question "as asked" (how to get the output in a better format), but Peter is right, if you want to get the amount of time it takes to run a cmdlet or script, you really should be using Measure-Command Commented Jul 23, 2013 at 16:47
  • @adi inbar thanks for your encouraging. I believe yours works too. Just 2 different approaches for the same thing. Commented Jul 23, 2013 at 21:30
  • Just for learning, there are another 2 good ways too: .NET Stopwatch object and "Get-History" way - they are really impressive: stackoverflow.com/questions/3513650/… Commented Jul 23, 2013 at 21:34

5 Answers 5

10

Use:

Measure-Command {copy-item $var0, $var1, $var2 Folders -force -recurse -verbose -Exclude name} | select @{n="time";e={$_.Minutes,"Minutes",$_.Seconds,"Seconds",$_.Milliseconds,"Milliseconds" -join " "}}

Measure-Command is to measure the command execution time. Use a hash table with "Select-Object" to customize the output.

The output is in my lab is (I am using the Get-Process cmdlet for testing):

time
----
0 Minutes 0 Seconds 16 Milliseconds

There is a "duration" property too. I will dig into it to see if that can simplify the things.

Sign up to request clarification or add additional context in comments.

Comments

3

Use .ToString() The default "c" "common" format looks pretty fine

function beep {
    # echo "`a" doesn't work in VSCode so I use this
    rundll32 user32.dll,MessageBeep
}

function time() {
    $command = $args -join ' '
    (Measure-Command { Invoke-Expression $command | Out-Default }).ToString()
    beep
}
> time sleep 1.23456
00:00:01.2520795

Comments

2

Just use the Minutes and Seconds properties instead of TotalMinutes and TotalSeconds. The Total- properties are double precision floating point numbers giving you the total timespan in the given unit of time, whereas the properties that are just the name of the unit (Days, Hours, Minutes, Seconds...) give you that unit's component of the timespan as an integer. So to get an output like "x minutes and y seconds", do this:

$elapsedTime = $endFulltime - $start
"{0} minute(s) and {1} second(s)" -f $elapsedTime.Minutes, $elapsedTime.Seconds

or if you prefer, make the second line:

"$($elapsedTime.Minutes) minute(s) and $($elapsedTime.Seconds) seconds"

Comments

2

I was looking for the same type of output as the OP.

To get what I wanted:

$script:start = get-date
$startFulltime = (Get-Date)
copy-item $var0, $var1, $var2 Folders -force -recurse -verbose -Exclude name
$endFulltime = (Get-Date)
(New-TimeSpan $startFulltime $endFulltime).ToString("hh'h:'mm'm:'ss's'")

The result will be something like:

02h:42m:12s

Comments

0

I'm, currently, learning PowerShell and I came across to something call measure-command, you can get more information here measure-command.

The basics are:

measure-command { your-command-here }

and it will give you a nice report about how long did your command take to be run, including information about how many Days, Hours, Minutes, Seconds, Ticks, etc, etc, etc.

Keep in mind that:

  1. The command is actually executed.
  2. There won't be any output for your command.

If you want/need just to know how many seconds (or days) did the command take to be executed you can put the measure-command cmdlet between parenthesis and ask for a given property. E.g:

(measure-command { New-Item -Path . -Name "test.txt" -ItemType "file" -Force }).TotalSeconds

That command creates a test.txt file (kind of touch a la Linux), there won't be any output, but the file will be created.
The only output will be the amount of Seconds the system took to create the file. E.g:

(measure-command { New-Item -Path . -Name "test.txt" -ItemType "file" -Force }).TotalSeconds
0.0007995

Longer commands will take longer (obvious) and give you some more interesting data.

Good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.