Home » Archive » Give Your Skype for Business PowerShell Scripts a UI In 10 minutes

Give Your Skype for Business PowerShell Scripts a UI In 10 minutes

First off, this is not really a Skype for Business post, and I do not pretend to be a Dev, so if you are looking for in depth coding then there are others out there who are way better than me. However, I wanted to share with you a method that can be easily adopted to give your PowerShell scripts a nice User Interface.

Previously, designing a UI in PowerShell meant you had to either write hundreds of lines of code referencing the Windows.Forms library, drawing out objects, assigning styles to them etc. etc. Or buy expensive software such as Sapien’s PowerShell Studio that did most of the heavy lifting for you, but you needed to be somewhat of a PowerShell wizard to create even basic scripts. Neither solution for me was ideal, PowerShell Studio just made my head hurt, while going old school and writing line by line was inefficient and often a simple script would bloat out from 50-60 lines of code to easily 2 or 300 lines. This of course may be down to my n00b coding skills but I imagine there are many people out there like me.

Looking around, I found a few projects that had made PowerShell modules you could reference to build your UI, but still there was a dependency on that module being installed on the system the script would eventually run on. I stumbled on a forum post that suggested to use Visual Studio to build your UI and then import it into your PowerShell script.

I have never used Visual Studio before, mainly because I am scared of it and didn’t really know where to start. Plus I am an impatient man, so I just wanted to get things working as fast as I could. However, I was astonished how easy it was.

The process is really simple. Create your UI in Visual Studio using.. you guessed it, a UI to design your UI, drag and drop, select component etc. Once complete copy the code to your PowerShell script, convert into variables, then reference the variables in your code to “do stuff”.

In a little more detail

    • Open Visual Studio and create a new WPF Project
    • Use Visual Studio to design your UI for your PowerShell Script
    • Use the Properties of each control to give them a unique name. This is what will be referenced later as a variable in PowerShell. It is important that you do NOT have any duplicates!
    • Once you have finished with your layout, click on the XAML tab below your design
    • Highlight and copy all the code in this window
    • Now it is time to open PowerShell ISE, or your favourite Editor. We need to parse the XAML from Visual Studio into a PowerShell Variable that we can reference. Add the following code at the beginning of your script
  • Now we need to place the XAML code into XML. We can do this by adding the following code to your script
    [xml]$XAML = @’’
  • Now you want to paste the XAML code from Visual Studio between the single quotes on the above code, like so
  • The next bit is important! You will notice that your XAML contains a few invalid parameters that cannot be read by PowerShell. The first parameter we need to get rid of is any reference to the following lines in the Window section:
  • Next we need to remove the x: before all the Name attributes e.g. change <TextBox x:Name=”txtSIPAddress” to <TextBox Name=”txtSIPAddress” for every reference (CTRL+H and find and replace)
    • There are other attributes / parameters that are incompatible I have found, so you will want to remove any references to the following:


      (remove these and their values)

    • Now the XAML is PowerShell friendly, we need to add a bit of code to parse the XAML into XML and then output a PowerShell Variables. To do this add the following code beneath the XAML
      $ReadXML=(New-Object System.Xml.XmlNodeReader $XAML)
      $Form=[Windows.Markup.XamlReader]::Load( $ReadXML )
      $XAML.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
    • Now when you run the PS Script, the names of the objects in the XAML can be referenced in PowerShell e.g. the textbox called txtSIPAddress in my example can be called by
      echo $txtSIPAddress


    • We can reference any parameter of the object. For instance if we wanted to programatically add some default text to that object we can dot reference it like so
      $txtSIPAddress.Text = “mark@myfluffy.cloud


    • You can use this then to build your UI and populated combo boxes, check boxes etc. All in the same manner.
    • You put your PowerShell code (the code that will do the heavy lifting in your script) into the click actions of the button objects. The cleanest way to do this is to place your PS code into a variable and then reference this variable in the button click action. Like so
      $enableAccount = { #Your Code goes here, multiple lines if necessary }
    • The last bit you need to do is show the UI. You do this at the very end of your PS Script with the following code
      $Form.ShowDialog()  | Out-Null
    • When you run your script you will get you nice new UI


Again, Dev Pro’s will probably be squirming at this method, I am sure there are better ways to code, but more me I am happy with this method for now, until I get more confidence and start to explore the art of the possible.

Hope this helps you on your journey!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: