Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

Practical Lessons on WPF Forms and PowerShell

Posted on March 4, 2018March 4, 2018 by Alan

Last weekend I decided that I was finally going to figure out how to stop using Windows Forms, and move to the newer Windows Presentation Foundation (WPF).  Some time ago, I had read a WPF blog post from FoxDeploy.com, which shows how you can cut and past code from Windows Visual Studio into their code to easily create a WPF form for PowerShell.

I have Visual Studio from work, but you can use the new Community Edition, too.  My first efforts at form design were frankly terrible. I went looking for a good tutorial on WPF, and found a great AngelSix.com YouTube video.  In the video, the developer walks through creation of an order form for a hypothetical manufacturing company: I watched the video, stopping every few minutes to recreate what was being done on screen.  I then saved the resulting XAML (pronounced “zamel”), and popped it into my FoxDeploy code.

It didn’t run.  The error handling from the FoxDeploy snipped was pretty generic.  I changed it to show the actual underlying error.  Watch the first 30 minutes of the video, stopping at adding codes and events.  Some notes:

    1. Create a WPF C# form.
    2. Remove <Grid></Grid> and replace with <StackPanel></StackPanel>
    3. Do not make the first line of code a comment.  The FoxDeploy RegEx will choke, and you won’t get anything
    4. Don’t put any events in the XAML
    5. You can add effects and color using the Properties panel.
    6. Don’t drag form objects into position in the designer.  The resulting code is terrible.

I am posting my version of the FoxDeploy code, together with my remarks within the XAML to provide some insight and guidance into how to get a handle with WPF forms.  For information on setting up events you will need to spend some additional research time.  The resulting effort is worth the work.  I used my new knowledge to update the splash screen for my Test-DCs.ps1 script.

Script Text

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy