PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Geek Sync Live

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Blog Posts

07-01-2016 - First nail in PowerShell’s coffin?
07-01-2016 - Creating WPF Windows
07-01-2016 - PowerShell Direct

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Creating WPF Windows

WPF is a great technique to create user interfaces in PowerShell. Most of the window content can be defined using XAML, a description similar to HTML but based on XML. The sample creates a "Fire Alarm" message window, and you can change text, font, size, and other details by simply editing the values inside the XAML description - no programming required:

Add-Type -AssemblyName PresentationFramework
  

$xaml = @'
<Window
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   SizeToContent="WidthAndHeight"
   Title="Get Out!"
   Topmost="True">
      <TextBlock
         Margin="50"
         HorizontalAlignment="Center"
         VerticalAlignment="Center"
         FontFamily="Stencil"
         FontSize="80"
         FontWeight="Bold"
         Foreground="Red">
         Fire Alarm!
      </TextBlock>
</Window>
'@

$reader = [System.XML.XMLReader]::Create([System.IO.StringReader]$XAML)
$window = [System.Windows.Markup.XAMLReader]::Load($reader)
  
$window.ShowDialog()

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.