Out-Gridview: run scripts when either Cancel or OK buttons pressed?

This topic contains 4 replies, has 3 voices, and was last updated by  Derf User 7 months ago.

  • Author
  • #84443

    Derf User

    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?

  • #84448

    Simon B

    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

  • #84454

    Derf User

    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.

  • #84457

    Simon Wåhlin

    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
  • #84464

    Derf User

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

You must be logged in to reply to this topic.