{"id":6131,"date":"2008-05-22T02:16:00","date_gmt":"2008-05-22T02:16:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/05\/22\/wpf-powershell-part-1-hello-world-welcome-to-the-week-of-wpf\/"},"modified":"2019-02-18T13:15:55","modified_gmt":"2019-02-18T20:15:55","slug":"wpf-powershell-part-1-hello-world-welcome-to-the-week-of-wpf","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/wpf-powershell-part-1-hello-world-welcome-to-the-week-of-wpf\/","title":{"rendered":"WPF &#038; PowerShell &#8211; Part 1 ( Hello World &#038; Welcome to the Week of WPF )"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Welcome to the Week of WPF.<span>&nbsp; <\/span>During the next 7 days, I\u2019ll help show you how you can use WPF and PowerShell together.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">PowerShell could always script almost everything in .NET, but, prior to the recent CTP2 you could not script Windows Presentation Foundation (WPF) in PowerShell.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Now you can script everything that WPF can do within PowerShell.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">This means you can write pretty sleek looking UIs in nice little PowerShell scripts.<\/font><\/p>\n<p class=\"MsoListBulletCxSpFirst\"><font face=\"Calibri\" size=\"3\">There are a lot of things you can do with this capability, but let\u2019s start things off simple.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Windows Presentation Foundation (WPF) is a set of .NET libraries for making next-generation user interfaces.<span>&nbsp; <\/span>In order to script WPF classes, you have to start them in a Single Threaded Apartment (STA).<span>&nbsp; <\/span>Luckily, starting in CTP2, you can run code in an STA a few ways in PowerShell.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">In order to script WPF, you have to do one of three things:<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Run the Script in Graphical PowerShell, where WPF scripts will run without any changes because Graphical PowerShell runspaces are STA.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Run the script in PowerShell.exe and add the \u2013STA script<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Create a background runspace that is STA, and run the script in the background runspace<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">In this post, I\u2019ll show you a \u201cHello World\u201d script, and how to run it in each of the three modes.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">First, let\u2019s write our \u201cHello World\u201d script.<span>&nbsp;&nbsp; <\/span>Open up Graphical PowerShell (gPowerShell.exe) and create a new script file (CTRL + N).<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Here\u2019s the Hello World file:<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$window = New-Object Windows.Window<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$window.Title = $window.Content = \u201cHello World.&nbsp; Check out PowerShell and WPF Together.\u201d<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$window.SizeToContent = \u201cWidthAndHeight\u201d<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$null = $window.ShowDialog()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Now in Graphical PowerShell, you should be able to just run it and be done.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">You can run the current script in Graphical PowerShell with F5 or the Green Run button in the upper left of the editor.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Now let\u2019s run the script in STA Mode PowerShell (Run PowerShell \u2013sta).<span>&nbsp; <\/span>Save it as \u201cHelloWorld.ps1\u201d<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Since PowerShell.exe \u2013sta doesn\u2019t load up WPF\u2019s assemblies, lets run these three lines to add the references:<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>Add-Type \u2013assemblyName PresentationFramework<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>Add-Type \u2013assemblyName PresentationCore<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>Add-Type \u2013assemblyName WindowsBase<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>.\\HelloWorld.ps1<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Now you have a Hello World in WPF from the console version of PowerShell.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Finally, you can embed scripts to generate WPF inside of an application or a background runspace.<span>&nbsp; <\/span>This has some advantages over the first two approaches\u2026 namely, without a background <\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">The code below will create a background runspace and set the two properties required to make WPF work in a PowerShell runspace, ApartmentState and ThreadOptions.<span>&nbsp; <\/span>ApartmentState determines if the runspace PowerShell is single or multithreaded (WPF requires single threaded), and if the same thread is used every time a command is run, or if a new thread is created each time.<span>&nbsp; <\/span>For WPF scripting to work in a runspace, you have to set ApartmentState to \u201cSTA\u201d and ThreadOptions to \u201cReuseThread\u201d.<span>&nbsp; <\/span>In the code below, the first 3 lines set up the runspace, the next 10 lines ensure that runspace is able to load WPF classes, and the final 4 lines run our original HelloWorld.ps1<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span># Create a runspace to run Hello World<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$rs.ApartmentState, $rs.ThreadOptions = \u201cSTA\u201d, \u201cReuseThread\u201d<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$rs.Open()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span># Reference the WPF assemblies<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd = {Add-Type}.GetPowerShell()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.SetRunspace($rs)<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.AddParameter(&#8220;AssemblyName&#8221;, &#8220;PresentationCore&#8221;).Invoke()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.Command.Clear()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd = $psCmd.AddCommand(&#8220;Add-Type&#8221;)<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.AddParameter(&#8220;AssemblyName&#8221;, &#8220;PresentationFramework&#8221;).Invoke()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.Command.Clear()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd = $psCmd.AddCommand(&#8220;Add-Type&#8221;)<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.AddParameter(&#8220;AssemblyName&#8221;, &#8220;WindowsBase&#8221;).Invoke()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$sb = $executionContext.InvokeCommand.NewScriptBlock(<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>(Join-Path $pwd &#8220;HelloWorld.ps1&#8221;)<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>)<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd = $sb.GetPowerShell()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$psCmd.SetRunspace($rs)<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><span>$null = $psCmd.BeginInvoke()<\/span><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">This is just a taste of some of the fun that PowerShell and WPF can have together, and how to get it working.<span>&nbsp; <\/span>You can now use everything in WPF in a PowerShell script.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">During the Week of WPF, I will post one script each day that will take us a little further down the rabbit hole of what PowerShell and WPF can do together.<span>&nbsp; <\/span>After the week of WPF is done, keep your eyes on this space, because every Wednesday, I\u2019ll try to post a \u201cWPF Wednesday Widget\u201d that uses showcase using PowerShell and WPF together.<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoListBulletCxSpMiddle\"><font face=\"Calibri\" size=\"3\">Hope this Helps,<\/font><\/p>\n<p class=\"MsoListBulletCxSpLast\"><font face=\"Calibri\" size=\"3\">James Brundage [MSFT]<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the Week of WPF.&nbsp; During the next 7 days, I\u2019ll help show you how you can use WPF and PowerShell together. PowerShell could always script almost everything in .NET, but, prior to the recent CTP2 you could not script Windows Presentation Foundation (WPF) in PowerShell. Now you can script everything that WPF can [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[96,248,295,322,360],"class_list":["post-6131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-add-type","tag-powershell","tag-runspace","tag-sta","tag-wpf"],"acf":[],"blog_post_summary":"<p>Welcome to the Week of WPF.&nbsp; During the next 7 days, I\u2019ll help show you how you can use WPF and PowerShell together. PowerShell could always script almost everything in .NET, but, prior to the recent CTP2 you could not script Windows Presentation Foundation (WPF) in PowerShell. Now you can script everything that WPF can [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6131"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}