If you are doing this as a script, you must have given yourself the permission execute scripts.
To start, you must load the library that holds the classes that are used for GUI programming. Remember, you cannot do a line break here. I only do it so that the whole line will visible.
[System.Reflection.Assembly]::Load("System.Windows.Forms, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b77a5c561934e089")
(Again, the above code is one single line.)
If you are unsure about versions and public keys, check your assembly folder in your Windows folder. If you right click on an assembly, and open the Properties dialog. There you highlight and copy the assambly information.
When the assembly is loaded, you can create a Form. This code will give it a caption and a size.
$myForm=New-Object Windows.Forms.Form $myForm.Size=New-Object Drawing.Size(360,200) $myForm.Text="Using Windows GUI from PowerShell"
The next thing I want to do, is to add a text label and a text input fileld on my form. I will position the text field under my label field.
$someLabel=New-Object Windows.Forms.Label $someLabel.AutoSize=$true $someLabel.Text="Enter your name:" $someLabel.Location=New-Object Drawing.Point(10,10) $someField=New-Object Windows.Forms.TextBox $someField.Size=New-Object Drawing.Size(300,16) $someField.Location=New-Object Drawing.Point(10,30) $myForm.Controls.Add($someLabel) $myForm.Controls.Add($someField)
To show this form, call the ShowDialog function of the form object.
This is the result:
The next thing to do, is to react on user input.