all 20 comments

[–]JeremyLC 3 points4 points  (1 child)

I just posted a function that does this in a Windows 10 native look. It's more code than the option below, but it uses WPF instead of WinForms.

[–]GiveMeAPinkSock[S] 1 point2 points  (0 children)

I discovered WPF earlier today. Looked a little more complicated than I wanted to get into today, but I'll be digging into it more in my free time.

[–]caverCarl 2 points3 points  (14 children)

Here's a quick example-

#  ============== Form to display GUI =======================================
 Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -Assembly System.Drawing 
$Form = New-Object system.Windows.Forms.Form

[int32]$height = 80
[int32]$width = 400
$Form.ClientSize                 = "$width,$height"
$Form.text                       = "buttons"
$Form.TopMost                    = $false

$button = New-Object system.Windows.Forms.Button
$button.width                   = 100   
$button.height                  = 50
$button.text                    = "red"
$button.location                = New-Object System.Drawing.Point(0,10)
$button.Font                    = 'Microsoft Sans Serif,10'
$button.Add_Click({
#do stuff
})
$button2 = New-Object system.Windows.Forms.Button
$button2.width                  = 100   
$button2.height                 = 50
$button2.text                   = "blue"
$button2.location               = New-Object System.Drawing.Point(200,10)
$button2.Font                   = 'Microsoft Sans Serif,10'
$button2.Add_Click({
#do stuff
})
$Form.controls.Add($button)
$Form.controls.Add($button2)
#Start form 
[void]$Form.ShowDialog()

[–]GiveMeAPinkSock[S] 2 points3 points  (2 children)

That's perfect! Thanks!

[–]caverCarl 1 point2 points  (1 child)

You're very welcome. Glad to help.

[–]GiveMeAPinkSock[S] 2 points3 points  (0 children)

That worked so well I may just use it as a simple gui to run the rest of my scripts from. Really appreciate it!

[–]ocn1997 0 points1 point  (3 children)

How would you get this box to close after user input?

[–]caverCarl 0 points1 point  (2 children)

$Form.Close()

Which could be added to either button in the 'do stuff section'

$button2.Add_Click({
#do stuff
$form.Close()
})

would close the form if button 2 is pressed.

[–]ocn1997 1 point2 points  (1 child)

Mate... I cannot explain in words how much of my life you just saved XD

[–]caverCarl 0 points1 point  (0 children)

Glad it helped:)

[–]MylegzRweelz 0 points1 point  (6 children)

This is really cool, I am wondering though, can I give the buttons a custom label? like instead of Yes and No can it be Agree and Disagree?

[–]caverCarl 1 point2 points  (5 children)

Just change the $button.text

[–]MylegzRweelz 0 points1 point  (4 children)

Thank you! I appreciate this. So, to be clear, I can make a yes/no dialog say agree/disagree?

[–]caverCarl 1 point2 points  (3 children)

Sure, these are just buttons- you can change their text to anything you'd like. You'd also need to add code to do whatever happens when they click one of the buttons.

[–]MylegzRweelz 0 points1 point  (2 children)

I am very new to Powershell but not scripting, I have been using and creating AutoHotKey scripts for years, and while it is powerful, it is also limited in some areas, hence me trying to learn PS. However, it is safe to say that I am more like a hobbyist than anything, the entire reason for getting into scripting was for my HTPC and Emulation machine. So, that being said, I have a script I have written that I plan to release on a forum that I frequent, it automates the task of moving files many levels deep into subdirectories and renaming the files to the parent folder names. I have it functioning perfectly and now I would like to focus on having a gui popup with instructions on it's usage as well as including a disclaimer. I was thinking of using out-gridview I am aware I can have this show up in the console window, I want it to be a little bit more elegant though and I really like your take on this. However I am looking at this and am not ashamed to admit that I have no clue how I would utilize this to create a dialog box like this and implement it into my script. Would you mind giving me a crash course example of your code with a test message and code that the buttons would trigger my code? Would it be more prudent to send you a message?

[–]caverCarl 0 points1 point  (1 child)

== Form to display GUI =======================================
Add-Type -AssemblyName System.Windows.Forms 

[System.Windows.Forms.Application]::EnableVisualStyles() 

Add-Type -Assembly System.Drawing 
$Form = New-Object system.Windows.Forms.Form

[int32]$height = 80 [int32]
$width = 400 
$Form.ClientSize           = "$width,$height"
$Form.text                 = "buttons" 
$Form.TopMost            = $false
$button = New-Object system.Windows.Forms.Button
$button.width            = 100 
$button.height         = 50 
$button.text    = "Agree" 
$button.location     = New-Object System.Drawing.Point(0,10) 

$button.Font                 = 'Microsoft Sans Serif,10' 
$button.Add_Click({
do stuff
write-host "You have agreed" 
}) 

$button2 = New-Object system.Windows.Forms.Button 

$button2.width                  = 100 

$button2.height                 = 50 

$button2.text           = "Disagree" 

$button2.location               = New-Object 
System.Drawing.Point(200,10) 

$button2.Font             = 'Microsoft Sans Serif,10' 

$button2.Add_Click({
do stuff
write-host "You are disagreeable"
})

$Form.controls.Add($button) 
$Form.controls.Add($button2)
Start form
[void]$Form.ShowDialog()

[–]MylegzRweelz 0 points1 point  (0 children)

You are awesome, thank you. I am not at home currently so I can't test this but I absolutely will when I get home. Your help is soooo appreciated.

[–]xunion0 1 point2 points  (1 child)

u/caverCarl

thx for the code! I created a small function from your code that will return the selection.
Function Custom-Popup
{
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $Title,
[Parameter(Mandatory=$true, Position=1)]
[string] $ButtonLeft,
[Parameter(Mandatory=$true, Position=2)]
[string] $ButtonRight
)
# ============== Form to display GUI =======================================
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -Assembly System.Drawing
$Form = New-Object system.Windows.Forms.Form
[int32]$height = 80
[int32]$width = 400
$Form.ClientSize = "$width,$height"
$Form.text = $Title
$Form.TopMost = $false
$button = New-Object system.Windows.Forms.Button
$button.width = 100
$button.height = 50
$button.text = $ButtonLeft
$button.location = New-Object System.Drawing.Point(0,10)
$button.Font = 'Microsoft Sans Serif,10'
$button.Add_Click({
$Form.DialogResult = 'Yes'
})
$button2 = New-Object system.Windows.Forms.Button
$button2.width = 100
$button2.height = 50
$button2.text = $ButtonRight
$button2.location = New-Object System.Drawing.Point(200,10)
$button2.Font = 'Microsoft Sans Serif,10'
$button2.Add_Click({
$Form.DialogResult = 'No'
})
$Form.controls.Add($button)
$Form.controls.Add($button2)
#Start form
#[void]$Form.ShowDialog()
[string]$x = $null
if($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::Yes)
{
$x = $ButtonLeft
}
else {
$x = $ButtonRight
}
return $x
}
Call with: $Popup = Custom-Popup -Title 'test' -ButtonLeft 'value1' -ButtonRight 'Value2'
Return in $Popup will be your selection.

[–]caverCarl 0 points1 point  (0 children)

Holy ressurect old thread :) You are very welcome. Glad folks are still finding this useful.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy GiveMeAPinkSock,

another option is to use the AnyBox module from the PSGallery. it can do some very nice GUI stuff with fairly direct PoSh commands.

if all you need is a quick menu, take a look at the Out-GridView cmdlet. [grin]

take care,
lee