Search code examples
formsxamlpowershell

"Are you sure?" via a messagebox in powershell


Goodmorning developers,

I am trying to make a delete function in powershell. I want to have something like this:

  function deleteEnv(){
    $result = [System.Windows.Forms.MessageBox]::Show('Are you sure?''Yes', 'No' 'Info')
    if(yes){
     //Delete
}

else {
//do nothing
}
}

When I click the delete button a message box must be shown first with the two buttons yes or no. If yes then delete else do nothing. How can I do this?

Kind regards


Solution

  • [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $result = [System.Windows.Forms.MessageBox]::Show('Are you sure?' , "Info" , 4)
    if ($result -eq 'Yes') {
        do stuff
    }
    

    Additional reading: http://powershell-tips.blogspot.com.by/2012/02/display-messagebox-with-powershell.html