Home > Back-end >  Text parameter not working with Powershell Progress Bar
Text parameter not working with Powershell Progress Bar

Time:04-12

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()

CodePudding user response:

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
}
  • Related