{"id":50893,"date":"2010-03-24T00:01:00","date_gmt":"2010-03-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/24\/hey-scripting-guy-using-the-windows-presentation-foundation-powershell-kit-to-create-a-gui\/"},"modified":"2010-03-24T00:01:00","modified_gmt":"2010-03-24T00:01:00","slug":"hey-scripting-guy-using-the-windows-presentation-foundation-powershell-kit-to-create-a-gui","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-using-the-windows-presentation-foundation-powershell-kit-to-create-a-gui\/","title":{"rendered":"Hey, Scripting Guy! Using the Windows Presentation Foundation PowerShell Kit to Create a GUI"},"content":{"rendered":"<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><b><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/b><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><b>About the author<\/b>: Doug Finke is a Microsoft PowerShell MVP working for <\/font><\/font><a href=\"http:\/\/www.lab49.com\/\"><span><font size=\"3\" face=\"Calibri\">Lab49<\/font><\/span><\/a><font size=\"3\" face=\"Calibri\">, a company that builds advanced applications for the financial service industry. Over the last 20 years, Doug has been a developer and author working with numerous technologies. You can catch up with Doug on <\/font><a href=\"http:\/\/www.dougfinke.com\/blog\/\"><span><font size=\"3\" face=\"Calibri\">his blog<\/font><\/span><\/a><font size=\"3\"><font face=\"Calibri\">.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">The Scripting Guys recently posted <\/font><a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/28\/hey-scripting-guy-february-28-2010.aspx?utm_source=twitterfeed&amp;utm_medium=twitter\"><font size=\"3\" face=\"Calibri\">Weekend Scripter: Creating a GUI for a Windows PowerShell Script<\/font><\/a><font size=\"3\"><font face=\"Calibri\">, in which Ed Wilson builds a Windows form GUI application for a Windows PowerShell script. In today&rsquo;s post, I&rsquo;ll build the same GUI application but with the Windows Presentation Foundation PowerShell Kit (WPK).<\/p>\n<p><\/font><\/font><\/p>\n<h3><span><font size=\"4\">About WPK<\/font><\/span><\/h3>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">WPK allows you to build rich user interfaces entirely with Window PowerShell scripts. WPK provides more than 600 cmdlets, which allow you to add WPF buttons, grids, list boxes, and more to Windows forms. <\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">James Brundage, author of WPK, says &ldquo;WPK pays homage to Tcl\/Tk.&rdquo; Tcl (pronounced &ldquo;Tickle&rdquo;) is a programming language created in 1990 by John Ousterhout when he was a professor at the University of California, Berkeley.<\/p>\n<p><\/font><\/font><\/p>\n<h3><span><font size=\"4\">Hello World in WPK<\/font><\/span><\/h3>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">You need to download and install the <\/font><a href=\"http:\/\/code.msdn.microsoft.com\/PowerShellPack\"><font size=\"3\" face=\"Calibri\">Microsoft PowerShell Power Pack <\/font><\/a><font size=\"3\"><font face=\"Calibri\">to try out these examples.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">WPK provides access to the presentation layer in WPF, surfacing controls in the standard Verb-Noun form. This example shows how you can add a label to a window and display, &ldquo;Hello World.&rdquo; In addition, each control has parameters. For the <b>New-Window<\/b> cmdlet, we are using four parameters: <b>Height<\/b>, <b>Width<\/b>, <b>Show<\/b>, and <b>Content<\/b>. In the example, the <b>Content<\/b> parameter is implied so that we can shorten the script by leaving it out. It would come after the <b>Show<\/b> parameter.<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><b><font size=\"3\"><font face=\"Calibri\"><br \/>Import-Module WPK<\/p>\n<p><\/font><\/font><\/b><\/p>\n<p class=\"MsoNormal\"><span>New-Window -Height 125 -Width 300 -Show {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>New-Label &#8220;Hello World&#8221; -FontSize 50<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>}<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><font size=\"3\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><img decoding=\"async\" title=\"Image of Hello World window\" alt=\"Image of Hello World window\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0324\/helloworld.jpg\" width=\"301\" height=\"123\"><\/p>\n<p><\/font><\/font><\/p>\n<p><span><\/span><\/p>\n<p class=\"MsoNormal\"><b><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/b><\/p>\n<h3><span><font size=\"4\">The Trip Cost WPK GUI<\/p>\n<p><\/font><\/span><\/h3>\n<p class=\"MsoNormal\"><span><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><strong><img decoding=\"async\" title=\"Image of Trip Cost WPK GUI\" alt=\"Image of Trip Cost WPK GUI\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0324\/tripcostgui.jpg\" width=\"301\" height=\"200\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0323\/tripcostgui.jpg\"><\/a><\/strong><\/font><\/font><\/p>\n<h3><span><br \/><font size=\"4\">The Code<\/font><\/span><\/h3>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">First, you need to <\/font><a href=\"http:\/\/code.msdn.microsoft.com\/PowerShellPack\"><font size=\"3\" face=\"Calibri\">download WPK<\/font><\/a><font size=\"3\"><font face=\"Calibri\"> from the MSDN Code Gallery. WPK lets you create rich user interfaces quick and easily from Windows PowerShell. Think HTA, but easy. <\/font><\/font><\/p>\n<p class=\"MsoNormal\"><b><font size=\"3\"><font face=\"Calibri\"><br \/>Import-Module WPK<\/p>\n<p><\/font><\/font><\/b><\/p>\n<p class=\"MsoNormal\"><span>New-Window -Title &#8220;Trip Cost&#8221; -WindowStartupLocation CenterScreen `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>-Width 300 -Height 200 -Show {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-Grid -Rows 32, 32*, 32*, 32* -Columns 100, 100* {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>New-Button -Content &#8220;_Calculate&#8221; -Row 0 -Column 0 -Margin 3 -On_Click {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$txtTotalCost<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>= $Window | Get-ChildControl txtTotalCost <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$txtMiles<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>= $Window | Get-ChildControl txtMiles <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$txtMilesPerGallon = $Window | Get-ChildControl txtMilesPerGallon <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$txtCostPerGallon<span>&nbsp; <\/span>= $Window | Get-ChildControl txtCostPerGallon <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$txtTotalCost.Text = `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8220;{0:n2}&#8221; -f (($txtMiles.Text \/ $txtMilesPerGallon.Text) `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>* $txtCostPerGallon.Text)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>}<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-\nTextBox -Name txtTotalCost `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 0 -Column 1 -Margin 3<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-TextBlock -Text &#8220;Miles&#8221; `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 1 -Column 0 -VerticalAlignment Center -Margin 3<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-TextBox -Name txtMiles `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 1 -Column 1 -Margin 3 -Text 100<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-TextBlock -Text &#8220;Miles Per Gallon&#8221; `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 2 -Column 0 -VerticalAlignment Center -Margin 3<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-TextBox -Name txtMilesPerGallon `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 2 -Column 1 -Margin 3 -Text 23<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-TextBlock -Text &#8220;Cost Per Gallon&#8221; `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 3 -Column 0 -VerticalAlignment Center -Margin 3<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>New-TextBox -Name txtCostPerGallon `<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span>-Row 3 -Column 1 -Margin 3 -Text 2.50<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span>}<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>}<\/p>\n<p><\/span><\/p>\n<h2><span><br \/><font size=\"4\">The Code Explained<\/font><\/span><\/h2>\n<\/p>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\">WPK is a Windows PowerShell module. To use a module, you first need to import it using the <b>Import-Module<\/b><i> <\/i>cmdlet. <\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\">The Trip Cost GUI uses these WPK cmdlets:<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\"><span>&nbsp;<\/span><\/p>\n<p><\/font><\/font><\/span><\/p>\n<table class=\"MsoTableGrid\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"108\">\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\"><font size=\"3\">New-Window<\/p>\n<p><\/font><\/font><\/span><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"330\">\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\">Creates a new <b>System.Windows.Window<\/b><\/p>\n<p><\/font><\/font><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"108\">\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\"><font size=\"3\">New-Grid<\/p>\n<p><\/font><\/font><\/span><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"330\">\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\">Creates a new <b>System.Windows.Controls.Grid<\/b><\/p>\n<p><\/font><\/font><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"108\">\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\"><font size=\"3\">New-Button<\/p>\n<p><\/font><\/font><\/span><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"330\">\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\">Creates a new <b>System.Windows.Controls.Button<\/b><\/p>\n<p><\/font><\/font><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"108\">\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\"><font size=\"3\">New-TextBox<\/p>\n<p><\/font><\/font><\/span><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"330\">\n<p class=\"MsoNormal\"><span><font face=\"Calibri\"><font size=\"3\">Creates a new <b>System.Windows.Controls.TextBox<\/b><\/p>\n<p><\/font><\/font><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"108\">\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\"><font size=\"3\">New-TextBlock<\/p>\n<p><\/font><\/font><\/span><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"330\">\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; About the author: Doug Finke is a Microsoft PowerShell MVP working for Lab49, a company that builds advanced applications for the financial service industry. Over the last 20 years, Doug has been a developer and author working with numerous technologies. You can catch up with Doug on his blog. &nbsp; The Scripting Guys recently [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[78,71,56,3,45,79],"class_list":["post-50893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-doug-finke","tag-graphical","tag-guest-blogger","tag-scripting-guy","tag-windows-powershell","tag-wpk"],"acf":[],"blog_post_summary":"<p>&nbsp; About the author: Doug Finke is a Microsoft PowerShell MVP working for Lab49, a company that builds advanced applications for the financial service industry. Over the last 20 years, Doug has been a developer and author working with numerous technologies. You can catch up with Doug on his blog. &nbsp; The Scripting Guys recently [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=50893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=50893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}