Search notes:
Create a Message Box with PowerShell
The following example tries to demonstrate how the
WinAPI function
MessageBox
(defined in
user32.dll
) can be called from
PowerShell.
add-type -typeDefinition @"
using System;
using System.Runtime.InteropServices;
public static partial class user32 {
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern Int32 MessageBox(
IntPtr hWnd,
String text,
String caption,
int options
);
}
"@
$chosen = [user32]::MessageBox(0, 'Yes or no?' , 'Choose wisely', 4) # 4: MB_OKCANCEL
if ($chosen -eq 6) { # 6: ID_YES
write-host "Yeah!"
}
else { # 7: ID_NO
write-host "Nay!"
}
The message box in action:
See also
It is also possible to add WinAPI function with the cmdLet
add-type
.