O'Reilly logo

Windows PowerShell 4.0 for .NET Developers by Sherif Talaat

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building GUI with PowerShell

We have spoken enough about Windows PowerShell with .NET framework and how it leverages .NET framework capabilities to do many useful tasks. The last thing to mention in this chapter is how PowerShell can use the underlying .NET framework to build a GUI.

The following PowerShell code demonstrates how to use the regular .NET framework namespace to build a simple WPF form with a single button:

$form = new-object Windows.Forms.Form
$form.Text = "Main Form"

$button = new-object Windows.Forms.Button
$button.text="Close Me!"
$button.Dock="fill"
$button.add_click({$form.close()})

$form.controls.add($button)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()

The previous code will create the form illustrated in the following ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required