2

If I execute the script in the console the progress bar is shown perfectly, but if I convert the script to exe with ps2exe the progress bar is not shown.

How can I solve this issue? Thank you

$totalTimes = 10
$i = 0

for ($i=0;$i -lt $totalTimes; $i++) {
   $percentComplete = ($i / $totalTimes) * 100
   Write-Progress -Activity 'Doing thing' -Status "Did thing $i  times" -PercentComplete $percentComplete
   sleep 1
}

2 Answers 2

4

Have you tried with PS2EXE-GUI?

It "converts" PowerShell scripts to EXE Files, optionally as GUI applications:

.\ps2exe.ps1 -noconsole -inputFile 'test.ps1' -outputFile 'test.exe' 

If test.ps1 contains your sample code, running .\test.exe will show a GUI progress bar such as the following:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

the key here is the '-noConsole' without that the progressbar doesnt show
oh.. and eth latest version of ps2exe seems to be at github.com/MScholtes/PS2EXE
0

I ended up using a custom progress bar instead:

Remove-Variable * -ErrorAction SilentlyContinue

function progress_bar($pctg_complete, $label, $leftend = "[", $rightend = "]", $counter = "#", $filler = ".", $completed = $false, $color = "white", $bar_length = 10)
{
    $pctg_complete = [math]::ceiling($pctg_complete)  # making it a INT

    # here $bar_length can be adjusted to increase the size of the PROGRESS BAR

    if ($label -eq $null) # if no LABEL given then use this 
    {
        $label = "My Process"
    }
    if ($completed -eq $true)  # to CLOSE the progress bar
    {
        write-host "`r`b" -NoNewline
        $pctg_complete = 100
        $progress_bar = "$label $leftend" + ($counter*$bar_length*($pctg_complete/10)) + ($filler*$bar_length*((100 - $pctg_complete)/10)) + "$rightend $pctg_complete %"
        write-host $progress_bar -NoNewline -ForegroundColor $color

    }
    else
    {
        $progress_bar = "$label $leftend" + ($counter*$bar_length*($pctg_complete/10)) + ($filler*$bar_length*((100 - $pctg_complete)/10)) + "$rightend $pctg_complete %"
        write-host $progress_bar -NoNewline -ForegroundColor $color
        Start-Sleep -Seconds 1
        if ($pctg_complete -ne 100)
        {
            write-host "`r`b" -NoNewline

        }

    }

}

## SIMPLE USAGE
# simple progress bar with "#" counters and Label=Test
progress_bar -pctg_complete 10 -label "Test"
Start-Sleep -Seconds 1
progress_bar -pctg_complete 50 -label "Test"
Start-Sleep -Seconds 1
# should have one of this (or both) at end of your process, to close the progress bar
progress_bar -pctg_complete 100 -label "Test"
progress_bar -completed $true -label "Test"
Start-Sleep -Seconds 1

#OUTPUT:
#Test [##################################################..................................................] 50 %

## ADVANCED USAGE
# here giving percentage complete in decimals
# giving unicode characters ░ as filler and █ as counter
# also using "|" as the left and right end character
# also modifying the color to Green
progress_bar -pctg_complete 8.9 -label "Test" -counter "$( [char]0x2588 )" -filler "$( [char]0x2591 )" -leftend "|" -rightend "|" -color Green
Start-Sleep -Seconds 1
progress_bar -pctg_complete 45.6 -label "Test" -counter "$( [char]0x2588 )" -filler "$( [char]0x2591 )" -leftend "|" -rightend "|" -color Green
Start-Sleep -Seconds 1
progress_bar -pctg_complete 98.523 -label "Test" -counter "$( [char]0x2588 )" -filler "$( [char]0x2591 )" -leftend "|" -rightend "|" -color Green
progress_bar -completed $true -label "Test" -counter "$( [char]0x2588 )" -filler "$( [char]0x2591 )" -leftend "|" -rightend "|" -color Green
Start-Sleep -Seconds 1

#OUTPUT:
#Test |███████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| 46 %

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.