{"id":13361,"date":"2011-07-10T00:01:00","date_gmt":"2011-07-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/07\/10\/use-showui-to-easily-create-graphical-interfaces-for-powershell\/"},"modified":"2011-07-10T00:01:00","modified_gmt":"2011-07-10T00:01:00","slug":"use-showui-to-easily-create-graphical-interfaces-for-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-showui-to-easily-create-graphical-interfaces-for-powershell\/","title":{"rendered":"Use ShowUi to Easily Create Graphical Interfaces for PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use ShowUi, a free Windows PowerShell module, to simplify creating graphical interfaces for scripts.\n&nbsp;\nMicrosoft Scripting Guy Ed Wilson here. Today, I have a guest article from my good friend James Brundage. Here is a little bit about him. &nbsp;\nJames Brundage is the founder of <a href=\"http:\/\/www.start-automating.com\/\">Start-Automating<\/a>, a company dedicated to saving people time and money by helping them automate. Start-Automating offers Windows PowerShell training and custom Windows PowerShell and .NET Framework development. James formerly worked on the Windows PowerShell team at Microsoft. You can follow James on Twitter (@jamesbru) or on the <a href=\"http:\/\/blog.start-automating.com\/\">Start-Automating company blog<\/a>.\nTake it away, James!\n&nbsp;\nSome people say I love Windows PowerShell a little too much.\nI strongly believe that a script a day keeps Dr. Scripto away. I easily spend more time scripting with PowerShell than any other recreation activity (except maybe Xbox), and I take any statement along the lines of &ldquo;you can&rsquo;t do that in Windows PowerShell&rdquo; as a challenge.\nToday, I&rsquo;m going to showcase an answer to one of those &ldquo;you can&rsquo;t do that in Windows PowerShell&rdquo; lines.\nAn astoundingly large number of people don&rsquo;t know that you can write user interfaces in Windows PowerShell. Some small technical changes in Windows PowerShell 2.0 enabled scripting of a really cool UI platform called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Windows_Presentation_Foundation\">Windows Presentation Foundation<\/a> (WPF).\nIn June 2011, scripting WPF reached a whole new level of easy with the release of ShowUI. ShowUI is a Windows PowerShell module that makes it simple to write UI in Windows PowerShell with WPF. You can download ShowUI at <a href=\"http:\/\/showui.codeplex.com\/\">http:\/\/showui.codeplex.com\/<\/a>. Now, not only can you write user interfaces in Windows PowerShell, but they&rsquo;re almost always a lot shorter.\nIn This Weekend Scripter article, I&rsquo;ll show and explain a fun sample: <a href=\"https:\/\/www.youtube.com\/watch?v=JcLVbNnAm9A&amp;feature=related\">How to Write a Drag-and-Drop Video Player in 12 Lines of Script<\/a>. Here&rsquo;s the code:<\/p>\n<p style=\"padding-left: 30px\">New-Window -AllowDrop -On_Drop {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $videoPlayer.Source = @($_.Data.GetFileDropList())[0]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $videoPlayer.Play()<\/p>\n<p style=\"padding-left: 30px\">} -On_Loaded {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $videoPlayer.Source = Get-ChildItem -Path &#8220;$env:PublicVideosSample Videos&#8221; -Filter *.wmv |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Get-Random | Select-Object -ExpandProperty Fullname<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $videoPlayer.Play()<\/p>\n<p style=\"padding-left: 30px\">} -On_Closing {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $videoPlayer.Stop()<\/p>\n<p style=\"padding-left: 30px\">} -Content {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; New-MediaElement -Name VideoPlayer -LoadedBehavior Manual<\/p>\n<p style=\"padding-left: 30px\">} &ndash;asjob<\/p>\n<p style=\"padding-left: 30px\">&nbsp;\nIt&rsquo;s a pretty simple app. I create a window, and I allow it to accept dropped items. Whenever an item is dropped (<b>On_Drop<\/b>), I look into that items data and get out the list of files. Then I select the first one, and make the video player&rsquo;s source that item. Then, I start up the video player.\nWhenever a window is loaded, I go find all the sample videos, pick a random one, and make that the video player&rsquo;s source. I then start the video player. Whenever the window is closing, I make sure to stop the video player as well, or else the media actually keeps playing in an invisible window. The window only has a little bit of content. That content is a media element called <b>VideoPlayer<\/b>. This <b>MediaElement<\/b> is what&rsquo;s providing the value for <b>$videoplayer<\/b> everywhere in the script. Because we want to be able to stop and play the video manually, I set <b>LoadedBehavior<\/b> to <b>Manual<\/b>. To make sure that I can watch the videos and script at the same time, I launch my UI in the background with <b>&ndash;AsJob<\/b>.\nShowUI lets you build richer solutions than you ever thought possible with Windows PowerShell. Download it for yourself at <a href=\"http:\/\/showui.codeplex.com\/\">http:\/\/showui.codeplex.com\/<\/a>, and keep watching <a href=\"https:\/\/www.youtube.com\/StartAutomating\">our YouTube channel<\/a> for more videos. And the next time a colleague of yours doubts the power of Windows PowerShell, just say:\n&ldquo;Hey, I&rsquo;ve got a little video to show you.&rdquo;\n&nbsp;\n<b>Splatting and ShowUI<\/b>\nSplatting is a nifty technique that lets you use a hash table as the input for your scripts or cmdlets. I cover this in greater detail in A Series on Splatting (<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/18\/use-splatting-to-simplify-your-powershell-scripts.aspx\">Part 1<\/a>, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/19\/automatically-create-custom-powershell-functions.aspx\">Part 2<\/a>, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/20\/use-powershell-to-combine-multiple-commands-for-ease-of-use.aspx\">Part 3<\/a>, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/21\/packaging-net-framework-classes-into-windows-powershell-functions.aspx\">Part 4<\/a>, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/22\/make-windows-powershell-your-web-client.aspx\">Part 5<\/a>).\nThis example shows how we can make a frontend for the world&rsquo;s simplest function: <b>Out-LastNameFirstName<\/b>. Watch the <a href=\"https:\/\/www.youtube.com\/watch?v=HM2vc5KBKqk\">YouTube video<\/a>.<\/p>\n<p style=\"padding-left: 30px\">Import-Module ShowUI<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">function Out-LastNameFirstName<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; param($firstName,$lastName)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&#8220;$lastName, $firstName&#8221;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$in = New-UniformGrid&nbsp; -Columns 2 -ControlName Get-FirstAndLastName -Children {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8216;First Name&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; New-TextBox -Name FirstName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8216;Last Name&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; New-TextBox -Name LastName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8216; &#8216;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;New-Button &#8220;OK&#8221; -IsDefault -On_Click {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $parent | Set-UIValue -passThru | Close-Control&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p style=\"padding-left: 30px\">} -show<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Out-LastNameFirstName @in<\/p>\n<p style=\"padding-left: 30px\">&nbsp;\nThe function is simple enough. <b>Out-LastAndFirstName<\/b> will simply print out a <b>firstName<\/b> and <b>lastName<\/b> packed together in <b>lastName<\/b>, <b>firstName<\/b> format. The UI is also really simple, because of the magic of ShowUI.\n<b>$in<\/b> will set the window&rsquo;s value when we close. A <b>UniformGrid<\/b> is a control that will automatically put items in a uniformly arranged grid. By giving the grid two columns, I can guarantee that the odd-numbered items go on the left and the even-numbered items go on the right.\nThe grid has a <i>ControlName<\/i>: <b>Get-FirstAndLastName<\/b>. <i>&ndash;ControlName<\/i> is a parameter in ShowUI that lets you anchor all of the values to a particular part of the UI. <i>&ndash;ControlName<\/i> helps determine which variables are there in your event handler, and where the UI will get values.\n<b>&lsquo;First Name&rsquo;<\/b> is just a string, and, if you put a string inside a collection in ShowUI, it will automatically try to turn it into a label. The text box comes right after it, and will appear on the right side. By giving the text box a name, two things happen:&nbsp;<\/p>\n<ul>\n<li>A variable <b>$FirstName<\/b> will appear in all of the event handlers pointing to that object.<\/li>\n<li>Whenever we go to output the value of <b>Get-FirstAndLastName<\/b>, it will create an entry in a hash table which contains the text in the text box.&nbsp;<\/li>\n<\/ul>\n<p>The &nbsp;<b>&lsquo; &nbsp;&lsquo;<\/b> simply forces a space. This just helps alignment within the uniform grid. The <b>OK<\/b> button is where the magic is. When it is clicked, the automatic variable <b>$parent<\/b> (which points to the nearest <b>&ndash;ControlName<\/b>) is piped into a command <b>Set-UIValue<\/b>. At this point, <b>Set-UIValue<\/b> creates a value for the control based on what was typed in the text boxes.\n&nbsp;\nThanks again to James Brundage! I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b>\n&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use ShowUi, a free Windows PowerShell module, to simplify creating graphical interfaces for scripts. &nbsp; Microsoft Scripting Guy Ed Wilson here. Today, I have a guest article from my good friend James Brundage. Here is a little bit about him. &nbsp; James Brundage is the founder of Start-Automating, a company dedicated to saving people [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[71,56,184,3,4,170,45],"class_list":["post-13361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphical","tag-guest-blogger","tag-james-brundage","tag-scripting-guy","tag-scripting-techniques","tag-splatting","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use ShowUi, a free Windows PowerShell module, to simplify creating graphical interfaces for scripts. &nbsp; Microsoft Scripting Guy Ed Wilson here. Today, I have a guest article from my good friend James Brundage. Here is a little bit about him. &nbsp; James Brundage is the founder of Start-Automating, a company dedicated to saving people [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13361","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13361"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13361\/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=13361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}