Skip to content

Instantly share code, notes, and snippets.

@bender-the-greatest
Last active November 4, 2022 11:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bender-the-greatest/741f696d965ed9728dc6287bdd336874 to your computer and use it in GitHub Desktop.
Save bender-the-greatest/741f696d965ed9728dc6287bdd336874 to your computer and use it in GitHub Desktop.
Alternative implementation of Measure-Command, works similarly to Linux `time` command
# Alternative (improved?) implementation of Measure-Command
# Works similarly to the Linux/Unix `time` command
#
# Function which times how long a command takes to completion
# Note that this function outputs to Write-Host so as to
# protect the proper return value. Note that a time will be
# returned even if the command fails.
#
# Unless `-quiet` is specified, command output is sent directly
# to `Write-Host` to allow for the simultaneous return of the
# elapsed time object.
#
# Returns: DateTime object representing how much time elapsed
# for the duration of the command.
#
# Arguments:
# -quiet : Suppress output from command (the difference
# is still displayed and returned).
# -command : The command (as a string) you wish to time.
#
# Usage:
# time "echo hello world"
# time -q "echo The command runs but suppresses the output"
# $dateTimeObject = $(time "vagrant converge")
function time {
Param(
[Parameter(Mandatory=$true)]
[string]$command,
[switch]$quiet = $false
)
$start = Get-Date
try {
if ( -not $quiet ) {
iex $command | Write-Host
} else {
iex $command > $null
}
} finally {
$(Get-Date) - $start
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment