Author Posts

November 17, 2017 at 7:47 am

Powershell V5
I need to run a different script depending on which of the OK or Cancel buttons are clicked. Is there a simple solution please?

November 17, 2017 at 9:42 am

Can you share what you have so far ? Do you mean run script 1 if ok is clicked and run script 2 if cancel is clicked ? or are their multiple ok and cancel buttons on a form ?

See here for example of an on_click event

https://blogs.technet.microsoft.com/heyscriptingguy/2014/08/02/weekend-scripter-fixing-powershell-gui-examples/

November 17, 2017 at 12:04 pm

I am using a standard Out-Gridview (not WPF) and need to run 1 script when OK is clicked and another for the Cancel button. I can find no information anywhere about this.

November 17, 2017 at 12:25 pm

I guess you could do something like this:

$Selected = Get-Process | Out-GridView -PassThru
if($null -eq $Selected) {
    # Cancel was pressed
    # Run cancel script
}
else {
    # OK was pressed, $Selected contains what was chosen
    # Run OK script
}

November 17, 2017 at 1:40 pm

It works nicely and exits with the Cancel button or the X at the top. Thank you, Sir.