December 9, 2010

How to display a Yes/No message box ?

There isn’t a msgbox or an input function in Powershell, but you can directly call Windows.Forms.MessageBox  to do it. Thansks to Bart De Smet for his tutorial about that.

[reflection.assembly]::Load("System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089") | out-null

# Display a confrimation dialog
# Return$True if the user click 'Yes'
function ConfirmMessageBox {
		[String]$WinTitle='PowerShell Script',
		$MsgText='Do you really want to continue ?'
	$result = [Windows.Forms.MessageBox]::Show($MsgText, $WinTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question)
	If ($result -eq [Windows.Forms.DialogResult]::Yes) {
		Return $true
	Else {
		Return $false

No comments:

Post a Comment