Sometimes PowerShell scripts are a way to comlicated to present your hardly gathered data to end users that are not familiar with the command line. Therefore I recommend to present data by Windows WPF forms that could be achived without being a coding expert.
With the help of Visual Studio everyone is able to build WPF forms even without coding expertise. In this tutorial I will show you how to generate a User Information script displaying Hostname, Username and Domainname. This is a basic tutorial showing how to build a graphical user interface within minutes. The script can be extended easily, for example you can display “Model Type” of your Hardware, “Network Drives” connected, “Outlook Profile” size, installed software and a lot more!
Restart Visual Studio Code and reproduce the issue. Go into the 'Output' panel (Ctrl + Shift + U or Cmd + Shift + U). In the drop down on the right, select 'PowerShell Editor Services'. Copy the entire contents of the Output panel and paste it into the GitHub issue in the browser.
- PowerShell in Visual Studio Code PowerShell is a task-based command-line shell and scripting language built on.NET, which provides a powerful toolset for administrators on any platform.
- Before we can paste our Visual Studio XAML code into the PowerShell script we have to perform some small modifications: Change line 1 from.
- The PowerShell extension for Visual Studio Code. The Azure Functions extension for Visual Studio Code. Create your local project In this section, you use Visual Studio Code to create a local Azure Functions project in PowerShell.
- I’ve used VSCode as my primary Powershell environment for over 3 years and have tried a lot of extensions and settings, and now is your turn to learn from my.
Start a Project
Start Visual Studio (Express / Community / Professional) and open a “New Project …”
Result:
The result is an empty WPF form that will be extended within the next steps.
Add Labels, Textboxes and Close Button
Add four labels to your form as in the picture below. If the Toolbox on the left side isn’t available on your Visual Studio installation you could reopen this element by clicking “View –> Toolbox“.
Add a close button to your form.
Result:
Visual Studio will automatically generate the following XAML code that can be used in PowerShell.
To generate a WPF in PowerShell we can use the following script as a good start.
Before we can paste our Visual Studio XAML code into the PowerShell script we have to perform some small modifications:
- Change line 1 from<Window x:Class=”WpfApplication9.MainWindow”to <Window
- Remove line 6 and 7
- Line 10 to 17 – Renamex:Name=to Name=
After modification just paste the XAML code to your PowerShell script in line 4.
Result:
Save your PowerShell script and execute it. If you used PowerShell ISE to built your script just press F5 and you will see the following result.
Visual Studio Code Unix Sandbox
Congratulations you built your own PowerShell GUI!
Add Close Event
Now that we are able to display our form we should get the close button running first. We just have to assign a event to our button that is named “button” by default.
You are now able to close you application by clicking
Add System Variables
To receive the Hostname, Username and the Domain we can use the following commands:
Just add the following marked lines to your code and you are done with your first PowerShell WPF Form.
Result:
Feel free to modify your XAML code in Visual Studio to get better user experiences. As an example I’ve created the following XAML code.
Visual Studio Code Powershell 7
As you can see the result looks different to the form before and it just took a few minutes to achive this result in Visual Studio.