Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Create WPF XAML powershell GUI form with Visual studio

Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial

Last month in pune, India, I got an opportunity to present a small offline webinar for around 10 to 15 users, I was preparing for this session and couldn't write much on my blog in the month of February. This seminar was regarding building Powershell GUI using WPF. In this first part, I shown how to create a XAML GUI form, which I can easily use in powershell with few modification. Before designing a form I always use pen and paper to know, how my GUI will look like. Below is my ruff design, Sorry for the bad handwriting and straight lines :). 

Powershell gui code winform wpf, graphical user interface no code design build design think on paper

Here I wanted to show easier way to create and didn't want to code for the same, its time to create XAML code of above design, First of all, there is a requirement of Visual Studio Community Edition to be installed, It can be downloaded from https://www.visualstudio.com/vs/community. It is free. Below are the system prerequisite to install the software. 

Visual Studio Community 2015 with Update 3
Hardware Requirements
  • 1.6 GHz or faster processor
  • 1 GB of RAM (1.5 GB if running on a virtual machine)
  • 4 GB of available hard disk space5400 RPM hard disk drive
  • DirectX 9-capable video card (1024 x 768 or higher resolution)
Windows 10
Windows 8.1
Windows 8
Windows 7 SP 1
Windows Server 2012 R2
Windows Server 2012
Windows Server 2008 R2 SP1

Once software is launched, We will create a new WPF Application project by clicking File menu >> New >> Project. In the New Project, Under Templates>>Visual C#  Search for WPF Application (Type C#), This is a Windows Presentation Foundation Client application and click OK.

Microsoft windows Powershell, microsoft visual studio community, xaml wpf gui, graphical user interface powershell

This shows a Empty form on the design view, Expand ToolBox on the Left, then select the WPF controls, drag and drop them in the form, then Resize and align them accordingly. I have used 4 wpf controls here.
0: Label - Computer or IP
1: TexBox - Input Box
2: Button - This is action button, Once it is pressed information is processed
3: Grid - In this table information will be shown.

Microsoft Visual Studio 2015 community edition, Build and design powershell wpf winform xaml drag and drop controls button datagrid

Once all the controls in place, I want to give them user friendly and meaningful name which I will be using in Powershell when I start actual coding, for this choose properties of each control and provide it a name in the right hand side bottom corner, you can select color and other stuffs here, I am keeping everything default and as simple as possible with less options.

Once complete form is built, you can see the GUI code in bottom, copy this code. 

WIndows powershell, wpf xaml gui code build on visual studio community edition get event logs, button, gridview code and controls toolbox textbox

I can not use this complete code, and need to remove few lines and items from the code, first copy the code in notepad, I have highlighted in darkgrey, which needs to be removed. Lines can be removed straightly, another thing I don't require x: in front of Name, which I can replace all with empty, nothing. In the bottom their is usable code highlighted as yellow. This I will use in next article where actually I will write powershell codes.

xaml gui powershell code visual studio code, build gui form wpf powershell graphical user interface tools utility less code no code.png

I can write another PowerShell function to remove unwanted part from this, but I want to show here what we don't require here.

Other Powershell GUI blogs
COOL POWERSHELL FREE ONLINE GUI GENERATOR TOOL, POSHGUI
Generate random password GUI using powershell

Go Back

Comment

Blog Search

Page Views

11361905

Follow me on Blogarama