Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrowrpurr/a9dbb5d205cdf81e8f81334f979b20b3 to your computer and use it in GitHub Desktop.
Save mrowrpurr/a9dbb5d205cdf81e8f81334f979b20b3 to your computer and use it in GitHub Desktop.
UI Dialog Boxes (from Windows `.bat` BATCH files) using `PowerShell` commands (into .NET assemblies)

UI Dialog Boxes (from Windows .bat BATCH files)

using PowerShell commands (into .NET assemblies)

.bat header

@echo off
setlocal EnableDelayedExpansion

:: [REQUIRED for new lines in dialog text]
:: This sets up a delayed expansion !\n! variable for literal newlines, when needed.
(set \n=^
%=DO NOT REMOVE THIS LINE - This creates a variable for using newlines in PowerShell commands=%
)

Ok Dialog

image

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Simple "OK" popup dialog
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set MSGBOX_TEXT=^Line One
set MSGBOX_TEXT=^!MSGBOX_TEXT!!\n!Line Two

powershell -c "Add-Type -Assembly System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show(\"${env:MSGBOX_TEXT}\")"

Ok Dialog with title

image

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Simple "OK" popup dialog with a title
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set MSGBOX_TITLE=^Hello Dialog!

set MSGBOX_TEXT=^Line One
set MSGBOX_TEXT=^!MSGBOX_TEXT!!\n!Line Two

powershell -c "Add-Type -Assembly System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show(\"${env:MSGBOX_TEXT}\", \"${env:MSGBOX_TITLE}\")"

Yes/No Dialog

image

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: "Yes"/"No" choice popup dialog
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Available types: OK, OKCancel, YesNo, YesNoCancel, RetryCancel, CancelTryContinue, AbortRetryIgnore
set MSGBOX_TYPE=YesNo

set MSGBOX_TITLE=^Hello Dialog!

set MSGBOX_TEXT=^Line One
set MSGBOX_TEXT=^!MSGBOX_TEXT!!\n!Line Two

for /f "usebackq delims=" %%i in (`
    powershell -c "Add-Type -Assembly System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show(\"${env:MSGBOX_TEXT}\", \"${env:MSGBOX_TITLE}\", \"${env:MSGBOX_TYPE}\")"
`) do set MSGBOX_RESULT=%%i

echo ^Result: %MSGBOX_RESULT%

:: If your code is inside of an if() or for(), use !MSGBOX_RESULT! instead (delayed expansion)
echo ^Result: !MSGBOX_RESULT!

User Text Input Dialog

image

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Text input popup dialog
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set MSGBOX_TITLE=^Hello Dialog!

set MSGBOX_TEXT=^Line One
set MSGBOX_TEXT=^!MSGBOX_TEXT!!\n!Line Two

set MSGBOX_VALUE=^Default value

for /f "usebackq delims=" %%i in (`
    powershell -c "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::InputBox(\"${env:MSGBOX_TEXT}\", \"${env:MSGBOX_TITLE}\", \"${env:MSGBOX_VALUE}\")"
`) do set MSGBOX_RESULT=%%i

:: If the user selects "Cancel", the result will be an "" empty string
echo ^User Input: %MSGBOX_RESULT%

:: If your code is inside of an if() or for(), use !MSGBOX_RESULT! instead (delayed expansion)
echo ^User Input: !MSGBOX_RESULT!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment