Functions/timer.ps1

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

function Wait-ForMilliseconds([int]$ms = 300, [bool]$displayMessage = $false) {
    if ($displayMessage) {
        Write-Host "Waiting for ${seconds} ms and continue..."
    }
    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 sleep for 3 seconds"
        Wait-ForSeconds 3
    }
    if ($shouldExit) {
        exit
    }
}

function Wait-OrPressAnyKey([string]$prompt = "Press any key to continue or it continue after {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;
        }
    }
    return $true;
}