Hi all,

- Synopsis:

I wanted to introduce a function that I used in my daily work .

This function permit to reply to the following question :

" I want to know : How many time is required to enumerate my folder tree on my File Server"

So the first answer is, which basic PowerShell command enumerate a folder tree?

Get-ChildItem -directory -recurse

I used Stopwatch in my code cause this is inherited from .NET and by logic more robusts and efficients than the classic measure-command CmdLet.

- Quick Help

The "$SW" variable control the timer execution (StartNew,Stop,ElapsedTime) full methods availables are here :


The command used in my test is ran by the "$Block" variable you can replace this command by any command or scriptblock.

Some usage examples can be found here :


The full code is available below:

function Watch-TimeElapsed {

# Start Time command

$sw = [Diagnostics.Stopwatch]::StartNew()

# Place here the scriptblock you want to measure

$block = gci -recurse -directory -errorAction SilentlyContinue -ErrorVAriable +Global:StrError


# Stop Time



# Store Elapsed time into desired vars

$ms = $sw.Elapsed.Milliseconds

$min = $sw.Elapsed.Minutes

$hours = $sw.Elapsed.Hours

$MeasureItem = $block.Count

 # Display timer statistics to host

"Total elapsed time in minutes: $min , in hours : $hours and milliseconds : $ms for a number of : $MeasureItem items" -ForegroundColor:Green

If ($strerror)


$ErrorCount = ($strerror).Count

Write-Warning "Error Variable contains some errors : $errorCount"

Write-Warning 'Use the following command to parse errors: "$strerror.Message.Exception"'


Else {

Write-Warning "End of process without errors"



Other Languages