Functions/timer.ps1

function Wait-ForSeconds([int]$seconds = 3, [bool]$displayMessage = $false) {
    if ($displayMessage) {
        Write-Host "It will continue in ${seconds} seconds..."
    }
    Start-Sleep -s $seconds
}

function Wait-ForMilliseconds([int]$ms = 300, [bool]$displayMessage = $false) {
    if ($displayMessage) {
        Write-Host "It will continue in ${ms} ms..."
    }
    Start-Sleep -m $ms
}

function Wait-ForKeyPress ([string]$message = "Press any key to continue…", [bool] $shouldExit = $false) {
    if ($psISE) {
        $Shell = New-Object -ComObject "WScript.Shell"
        $null = $Shell.Popup($message, 0, "Script Paused", 0)
    }
    elseif ($host.Name -cmatch "ConsoleHost") {
        Write-Host "$message" -ForegroundColor Yellow
        $null = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    else {
        Write-Host "$Message`nCurrently is running in '$($host.Name)' and not running in 'ConsoleHost', so it will wait for 3 seconds and continue"
        Wait-ForSeconds 3
    }
    if ($shouldExit) {
        exit
    }
}

function Wait-OrPressAnyKey([string]$prompt = "Press any key to continue or it will continue in {0} seconds", [int]$secondsToWait = 10) {
    Write-Host -NoNewline ($prompt -f $secondsToWait)
    $secondsElapsed = 0
    $millisecondsCounter = 0
    while ((!$host.ui.rawui.KeyAvailable) -and ($secondsElapsed -lt $secondsToWait) ) {
        start-sleep -m 10
        $millisecondsCounter += 10
        if ($millisecondsCounter -eq 1000) {
            $secondsElapsed++
            $millisecondsCounter = 0
            Write-Host -NoNewline "."
        }
        if ($secondsElapsed -eq $secondsToWait) {
            Write-Host "`r`n"
            return $false;
        }
    }
    Write-Host "`r`n"
    return $true;
}