Add-Type -AssemblyName System.Windows.Forms # Range to count through $Range = 1..100 # Determine minimum & maximum values of range $MinMax = $Range | Measure-Object -Minimum -Maximum #inizializzo counter a zero $Counter = 0 # Function to run on button click Function Start-Counting { # On button click we set progress bar to start (minimum) value and refresh $ProgressBar.Value = $MinMax.Minimum $ProgressBar.Refresh() $Button.Refresh() Foreach ($Number in $Range) { $Counter++ # Perform single step on progress bar and refresh $ProgressBar.PerformStep() $ProgressBar.Refresh() # Step interval Start-Sleep -Milliseconds 50 } $Counter = 0 } $ProgressForm = New-Object System.Windows.Forms.Form $ProgressForm.Text = "Operation in progress..." $ProgressForm.SizeGripStyle = "Hide" $ProgressForm.StartPosition = "CenterScreen" $ProgressForm.FormBorderStyle = "FixedDialog" $ProgressForm.MaximizeBox = $False $ProgressForm.Width = 400 $ProgressForm.Height = 200 $ProgressBar = New-Object System.Windows.Forms.ProgressBar $ProgressBar.Width = 340 $ProgressBar.Height = 25 $ProgressBar.Visible = $True $ProgressBar.Minimum = $MinMax.Minimum $ProgressBar.Maximum = $MinMax.Maximum $ProgressBar.Value = $MinMax.Minimum $ProgressBar.Step = 1 $ProgressBar.Location = New-Object System.Drawing.Point(20,60) $ProgressForm.Controls.Add($ProgressBar) $Button = New-Object System.Windows.Forms.Button $Button.Text = "Test Progress" $Button.Width = 200 $Button.Height = 40 $Button.Add_Click({Start-Counting}) $Button.Location = New-Object System.Drawing.Point(20,90) $Button.Font = "Microsoft Sans Serif,10" $ProgressForm.Controls.Add($Button) $ProgressForm.ShowDialog() $ProgressForm.TopMost = $True