0

I've been looking around for the past half hour on how to show the text inside of a GUI ProgressBar in Powershell, and everything I've tried has failed. I've even been referencing MSoft docs on it.

Am I doing something wrong? How do I add in the text?

This isn't my full script or exactly how I'll be using it - I just made an example so I could try to get it working.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = '500,300'
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true

$computerList = 'server01', 'server02', 'server03', 'server04', 'server05', 'server06', 'server07', 'server08', 'server09', 'server10'

$progressbar1 = New-Object System.Windows.Forms.ProgressBar
$progressbar1.Size = '300, 20'
$progressbar1.Location = '20,60'
$progressbar1.Text = "Processing..."
$progressbar1.Maximum = $computerList.Count
$progressbar1.Step = 1
$progressbar1.Value = 0

foreach ($computer in $computerList){    
    $progressbar1.PerformStep()
}

$form.Controls.Add($progressbar1)
$form.ShowDialog()
2
  • Please at least include where you defined $progressbar Commented Apr 8, 2022 at 21:15
  • @Theo I see what you're saying, I just added it in. I'm still not able to see any text on the bar itself though. Commented Apr 8, 2022 at 21:45

1 Answer 1

1

I guess the easiest way is to have a Label control above the progressbar and update the text in there:

$progressLabel = New-Object System.Windows.Forms.Label
$progressLabel.Size = '300, 20'
$progressLabel.Location = '20,40'
$progressLabel.Text = "Processing..."
$form.Controls.Add($progressLabel)

foreach ($computer in $computerList){  
    $progressLabel.Text = "Doing stuff on computer '$computer'.."  
    $progressbar1.PerformStep()
    # perform your action on $computer here
}
Sign up to request clarification or add additional context in comments.

5 Comments

that helps, but now I'm seeing that the progress bar isn't actually moving in real-time... it just shows the completed state after the loop is done.
@AaronJK That depends on what action you perform inside the foreach ($computer in $computerList). I guess yoy want to do something with each computer there no? Right now, there is no action at all, so the loop goes by extremely fast and you'll not have time to see what is displayed. For clarity, I added an inline comment.
Correct. I added in a simple Import-Csv and then added in a 'Start-Sleep' for 1 second in the foreach loop... to ensure nothing would be going too fast. The '$form.ShowDialog()' is still after the loop. So, what's happening is, the loop finishes then shows the dialog. If I place the form show dialog before, then it doesn't show the progress. I obviously don't want it inside the loop, as I'd get tons of forms lol.
@AaronJK Well, usually in a form some event is used to start an action like a button click ( in a button's Add_Click({..}) event handler) or the Activate event of the form itself. Now, the loop just runs somewhere in between the form and its controls definitions and only after that you do the ShowDialog() call.
I just added a click event and it's working now. Thank you!

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.