Progress Bar Tricks (Part 4)

by May 19, 2023

Due to popular request, here is a code that illustrates how you can use nested progress bars and show a “real” progress indicator for each task your code performs:

$servers = 'dc-01', 'dc-02', 'msv3', 'msv4'
$ports = 80, 445, 5985

$counterServers = 0
$servers | ForEach-Object {
    # increment server counter and calculate progress
    $counterServers++
    $percentServers = $counterServers * 100 / $servers.Count

    $server = $_
    Write-Progress -Activity 'Checking Servers' -Status $server -Id 1 -PercentComplete $percentServers

    $counterPorts = 0
    $ports | ForEach-Object {
        # increment port counter and calculate progress
        $counterPorts++
        $percentPorts = $counterPorts * 100 / $ports.Count


        $port = $_
        Write-Progress -Activity 'Checking Port' -Status $port -Id 2 -PercentComplete $percentPorts

        # here would be your code that performs some task, i.e. a port test:
        Start-Sleep -Seconds 1
    }
}

Tweet this Tip! Tweet this Tip!