Make progressbar appear on buttonclick in Windows Forms Powershell script?
Hello.

I am not very good at Powershell, so please bear with me if I ask questions which are easy to Powershell experts.
I have a Powershell script with 4 buttons, which each calls different .bat files I've written, to automatically backup my pictures and music and such, to my external harddrive and all that works perfectly.
Is there a way to make a marquee progressbar appear inside the window when a button is clicked? (strictly for good looks)

I have a code for a progressbar which works fine by itself, but can I make it appear when I click each button? Here is the code for the progressbar:

$progStrip = New-Object System.Windows.Forms.ProgressBar
$progStrip.Top = 70
$progStrip.Left = 10
$progStrip.Width = 515
$progStrip.Value = 0
$progStrip.Style="Marquee"
$progStrip.MarqueeAnimationSpeed = 50
$form.Controls.Add($progStrip)**

And here is my Powershell script with the button, that I want to trigger a progress bar, Marquee style for simplicity. Thank you very much in advance
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(550,210)
$form.Text = "System Backup"
$form.FormBorderStyle = "FixedDialog"
$form.StartPosition = "CenterScreen"
$form.KeyPreview = $True
$form.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$form.Close()}})

$icon = [system.drawing.icon]::ExtractAssociatedIcon('D:\Documents\Andet\Icons\icon1 (3).ico')
$form.Icon = $icon

$statStrip = New-Object System.Windows.Forms.StatusStrip
$statStrip.Items.Add("Backup via Powershell") | Out-Null
$form.Controls.Add($statStrip)

$1Button = New-Object System.Windows.Forms.Button
$1Button.Location = New-Object System.Drawing.Size(10,100)
$1Button.Size = New-Object System.Drawing.Size(125,40)
$1Button.Text = "Saved games backup"
$1Button.Add_Click({Start-Process "C:\Backup\Backup1.bat"})
$form.Controls.Add($1Button)

$2Button = New-Object System.Windows.Forms.Button
$2Button.Location = New-Object System.Drawing.Size(140,100)
$2Button.Size = New-Object System.Drawing.Size(125,40)
$2Button.Text = "Fuld backup til ekstern harddisk"
$2Button.Add_Click({Start-Process "C:\Backup\Backup2.bat"})
$form.Controls.Add($2Button)

$3Button = New-Object System.Windows.Forms.Button
$3Button.Location = New-Object System.Drawing.Size(270,100)
$3Button.Size = New-Object System.Drawing.Size(125,40)
$3Button.Text = "Slet og omdb filer/mapper"
$3Button.Add_Click({Start-Process "C:\Backup\Delete.bat"})
$form.Controls.Add($3Button)

$4Button = New-Object System.Windows.Forms.Button
$4Button.Location = New-Object System.Drawing.Size(400,100)
$4Button.Size = New-Object System.Drawing.Size(125,40)
$4Button.Text = "Afslut"
$4Button.Add_Click({$form.Close()})
$form.Controls.Add($4Button)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(185,15)
$objLabel.Size = New-Object System.Drawing.Size(200,50) 
$objLabel.Font = New-Object System.Drawing.Font("Arial",15)
$objLabel.Text = "Vlg backup type:"
$form.Controls.Add($objLabel) 
$form.Topmost = $True
$form.ShowDialog()

February 22nd, 2015 4:44pm

A progress bar nees much more code support from a script or a CmdLWet designed to be used with a pbar.  If the CmdLet is designed for a pbar the pbar will pop up automatically. YOu don't have to do anything.  If you have a script that requires bar support you need to write it inot your script.  There is no magic "just add this to a button and it works" routine.

Start here: http://www.sapien.com/blog/2011/07/14/primalforms-2011-spotlight-on-the-progressbar-control/

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 5:47pm

Thank you.

I have already looked at the website you linked.

The thing is, I want to activate the above mentioned Marquee progress bar, when I click a button. Remember, it doesn't have to do anything, no calculations or percentage or anything, just a marquee while the called .bat file is running xcopy, it's just to look good, I like that sort of thing.

I have found a workaround to this which works perfectly fine, which is calling 2 .bat files on a single button click: the one that runs xcopy, and then another .bat that calls for yet another PS1 script with a marquee progressbar in it, and then the xcopy uses "taskkill" to close the window with the progressbar by WindowTitle, but this involves 4 scripts running and I thought there had to be a simpler solution to this.

So there is no other (simple) way to specify a progressbar to appear on buttonclick?

February 23rd, 2015 2:17am

Yes there is a simpler solution.  Start by reading the blog articles on how to design and build PowerShell Forms.  Skip calling extra batch files and learn to use PowerShell correctly and it will be much easier.

I posted a solution to you taskkill request in another thread byt apparently you don't understand what a task is and how it I smanged in PowerShell.

Start by simplifying things.  Learn the basics and the rest will be very clear. 

At this point you have talked around about forms with buttons calling batch files calling forms.  None of this is how we would use PowerShell.  It is just a rube Goldberg you are building instead of actually trying to learn how to write a script.

Her is another blog article that will help you to understand how to design and build forms: http://www.sapien.com/blog/topics/user-interface-design-for-administrators/

Start by realizing that mixing batch files with PowerShell is a bad way to do anything.  Think about how Windows works when you use a program.  Look at more examples of how forms are designed.

As for your request - it is very hard to understand what you are asking or what you are actually needing to do.

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 3:14am

Hello.

Thank you for your quick reply.

You are right, I do not truly understand how how Powershell works, since I only started trying it out about 2 weeks ago, and had never actually tried it before that.

The reason I call a bat file instead of the actual Powershell, is because the bat file adds parameter "windowstyle hidden", which I do not know how to do, in another Powershell.

To put it shortly, what I want to do, is to open a window with a marquee progress bar while a .bat file with xcopy is running, and the close it again when xcopy is finished. I will look at the website you gave me, thank you.

By the way: as for my other thread, it is working now, but I have done nothing different, it just works out of the blue for no particular reason. I'm sorry for any inconvenience.

February 23rd, 2015 3:31am

You do not need to use a batch file to run XCOPY.  It will execute right in PowerShell. PowerShell can execute any Windows commands just like any CMD prompt.  PowerShell is the replacement for batch files.

If you need async processing in PowerShell we would use a Job and a timer.  The timer would poll the job for completion.

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 3:54am

Thank you very much for your detailed reply.

Can you please show me a quick example, of how I can run multiple xcopy after one another, like "xcopy c:\test1 c:\test2" and then "xcopy c:\test3 c:\test4" when I click button 1, how would I write this bit then:

Thank you in advance.

$1Button = New-Object System.Windows.Forms.Button
$1Button.Location = New-Object System.Drawing.Size(10,100)
$1Button.Size = New-Object System.Drawing.Size(125,40)
$1Button.Text = "Saved games backup"
$1Button.Add_Click({Start-Process "C:\Backup\Backup1.bat"})
$form.Controls.Add($1Button)
February 23rd, 2015 5:42am

Just run multiple Start-Process commands sequentially.

start-process -FilePath xcopy -wait -NoNewWindow -args 'c:\test d:\test /S'

You can run them as a job:

HELP Start-Job -Full

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 9:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics