Button Control - Yes No

Senior Member

Hi, I am working on form (with button Submit and Later) using PowerShell script. It opens a pop up when "Later" button on the form is clicked. The pop up has "Yes" and "No" buttons and the message says if you want to complete the form later. If yes is clicked the form closes but when no is clicked, I want the pop up close and the form to stay open. My script is as below:
#TODO: Place custom script here
Add-Type -AssemblyName PresentationCore, PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageBody = "Do you want to complete later?"
$MessageTitle = "Confirm"

$messageboxf = [System.Windows.MessageBox]::Show($MessageBody, $MessageTitle, $ButtonType, $MessageIcon)

switch ($msgBoxInput)
'Yes' {
$dt = Get-Date -Format "yyyyMMddHHmmss"
"User $($env:USERNAME.Text) deferred filling out form $($env:COMPUTERNAME) Date: $dt" | Out-File -FilePath "$($Global:REPO)$($env:USERNAME.Text)-$($env:COMPUTERNAME)-$dt.deferred" -force

'No' {
## Do something
$formAssetForm.visible = $true

With this script, when I click on No, the form still closes. Can someone please help what am I doing wrong here?

Thanks in advance

1 Reply
best response confirmed by hkaur2505 (Senior Member)


Hi I wouldn't use the "system yes no"

build your own yes no Button: 


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Windows Powershell Form by Flo'
$form.Size = New-Object System.Drawing.Size(720,360) 

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,100)
$Button.Size = New-Object System.Drawing.Size(280,20)
$Button.Text = "No"

$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(300,100)
$Button1.Size = New-Object System.Drawing.Size(280,20)
$Button1.Text = "Yes"

    [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")

    [System.Windows.Forms.MessageBox]::Show("No world." , "My Dialog Box");
    start calc.exe

$form.Topmost = $True
[void] $form.ShowDialog()