Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Script Timer

Posted on December 6, 2019December 6, 2019 By David Kittell

There are a few methods for this but my preference is below.

<#
.SYNOPSIS
    Functions to calculate the time it takes to run a script
.DESCRIPTION
    This script contains functions that will calculate the time it takes to run a given script in hours, minutes, seconds and milliseconds
.Example
    .".\Functions\ScriptTimer.ps1"
    $ScriptTimerStartTime = Start-ScriptTimer
    Get-Process | Format-Table -AutoSize
    Stop-ScriptTimer $ScriptTimerStartTime
#>


function Start-ScriptTimer
{
    $ScriptTimerStartTime = $(get-date)
    return $ScriptTimerStartTime
}

function Format-TimeSpan
{
    PARAM (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [TimeSpan]$TimeSpan
    )

    #Current implementation doesn't handle days.

    #By including the delimiters in the formatting string it's easier when we concatenate in the end
    $hours = $TimeSpan.Hours.ToString("00")
    $minutes = $TimeSpan.Minutes.ToString("\00")
    $seconds = $TimeSpan.Seconds.ToString("\00")
    $milliseconds = $TimeSpan.Milliseconds.ToString("\000")

    Write-Output ($hours + " hours " + $minutes + " minutes " + $seconds + " seconds " + $milliseconds + " milliseconds")
}

function Stop-ScriptTimer ([DateTime] $ScriptTimerStartTime)
{
    $ScriptTimerElapsedTime = $(get-date) - $ScriptTimerStartTime
    $ScriptTimerTotalTime = Format-TimeSpan $ScriptTimerElapsedTime.Ticks
    Write-Output "Script completed in $ScriptTimerTotalTime" | Out-String
}

<#
$ScriptTimerStartTime = Start-ScriptTimer
Get-Process | Format-Table -AutoSize # What ever you want to time would go here.
Stop-ScriptTimer $ScriptTimerStartTime
#>
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PowerShell Windows

Post navigation

Previous post
Next post

Related Posts

Allow Root SSH

Posted on August 16, 2016August 16, 2016

NOTE: Typically not a great idea for permanent production systems cat /etc/ssh/sshd_config | grep PermitRootLogin sed -i "s|PermitRootLogin no|PermitRootLogin yes|" /etc/ssh/sshd_config service sshd restart cat /etc/ssh/sshd_config | grep PermitRootLogin cat /etc/ssh/sshd_config | grep PermitRootLogin sed -i "s|PermitRootLogin yes|PermitRootLogin no|" /etc/ssh/sshd_config service sshd restart cat /etc/ssh/sshd_config | grep PermitRootLogin All information…

Read More

PowerShell Special Folders

Posted on September 17, 2015

$SpecialFolders = @{} $names = [Environment+SpecialFolder]::GetNames([Environment+SpecialFolder]) foreach($name in $names) { if($path = [Environment]::GetFolderPath($name)){ Write-Host $name "`r`n" $path } } Named Special Folders: Desktop Programs Personal MyDocuments Favorites Startup Recent SendTo StartMenu MyMusic DesktopDirectory Templates ApplicationData LocalApplicationData InternetCache Cookies History CommonApplicationData System ProgramFiles MyPictures CommonProgramFiles All information on this site is…

Read More

PowerShell – Get WebAdministration

Posted on March 14, 2016

If you are on a newly installed server and trying any of the IIS PowerShell scripts on this site that call “Import WebAdministration” you may have some issues if the “WindowsPowerShellWebAccess” feature isn’t installed, this script below should help. clear try { $ImportSucceeded = import-module WebAdministration -ErrorAction Stop } catch…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
©2025 David Kittell | WordPress Theme by SuperbThemes