PowerShell is a command-line tool but did you know it can also be used as a base for graphical interfaces? Sometimes command-line isn’t the best kind of interface for a particular instance. Building a PowerShell GUI for for your service desk is a great example. This is one of those times when it is more appropriate to build graphical tools instead.
PowerShell can use and expose .NET functionality and features. It’s possible to write GUI front ends for the scripts you create. Building PowerShell GUIs may seem complicated, especially if you are a beginner. Definicion de hardware y software yahoo. But if you have basic experience with PowerShell scripting then there’s no reason for you not to learn and adapt the practice of creating GUI for your scripts.
In this post, you will learn how to create a PowerShell GUI using the Windows Presentation Framework (WPF).
Prerequisites
Microsoft Visual Studio has simple, yet complex methods that are used to create a graphical user interface that has its ease of use range from easy to hard. With the pre-generated code, variables from the form, and different methods to have a less cluttered interfaces, the user can achieve a great amount of application functionality. A GUI front-end for creating and editing Inno Setup Scripts. Includes run-time debugging support. Install Designer (not free) unSigned Softworks: An easy-to-use GUI which helps you design custom forms. DwinsHs: Han-soft.
Before you dive in, please be sure you meet the following requirements:
- Visual Studio 2017 or later – You’ll use this to create the graphical user interface using WPF. You can download a free/community version.
- A script editor – I use Visual Studio Code, but you can also use another text editor of your choice. Some other options are Notepad++ and the built-in PowerShell ISE
- A Windows 10 computer with Windows PowerShell v5.1.
Build the Script
In this post, you’ll create a simple script named Main.ps1. This script will pull disk information from a local or remote system by querying the Win32_LogicalDisk WMI class.
You’ll need a script to wrap a GUI around first. I’ve chosen to use a script that allows you to provide a computer name and query disk information. This is, by no means, necessary to build a GUI though. Use the techniques you learn in this post to adapt your GUIs to your own scripts.
As an example script, I’ll create a function that performs the following actions:
- Accept input for the name of the computer to query
- Query the computer and store the fixed disks information to a variable
- Return the results
The Function
Below is the function you’ll use for this project, aptly named
Get-FixedDisk
. This project’s purpose is to get the information about the non-removable or fixed disks on the target machine.While this piece of code can be used as is, creating a GUI would be beneficial if you just want to perform a quick query without having to dot source the function and manually typing in the commands each time.
You can see that I’ve added a param() block in the code. This is to instruct the function to accept inputs based on the type of data indicated.
![Form gui editor patch free Form gui editor patch free](https://ugotsta.files.wordpress.com/2015/06/introjucer-gui-form-editor.png)
In the example, I’ve added a
Computer
parameter which accepts a string value. Also, by adding the Mandatory
parameter attribute, it ensures that the function does not run if the Computer
parameter is not specified at runtime.Next, line 18 shows the actual WMI query command which gets the list of all logical disks and saves the results to a variable named
$DiskInfo
. I’ve also added a filter to get only the disks with DriveType=3
. This filter ensures that only the information about local fixed disks is displayed.Dot Sourcing
At this point, you now have a working script and are ready to test it. But before you can test the script, you need to import the code into a PowerShell session. One way of loading code into a PowerShell session is by dot sourcing.
To dot source a script, type a dot (
.
) and a space before the script path. If the script were in the C:PoshGUI-sample folder, you could dot source it like below.You can also specify the full path if you’re no in the current working directory.
Now that we have imported the code into the memory, we can proceed with testing the function we’ve created. In the below example, it shows that the
Get-FixedDisk
function is used to query the computer poshLabExc.Build the GUI
At this point, you’ve created the script file named Main.ps1, and inside the script created the function
Get-FixedDisk
. You were also able to test and confirm that the function is working.Now you can start building the GUI.
First plan how you’d like the GUI to look and the elements you’d like to use. For this simple example, our GUI will have:
- a text box where the computer name can be entered
- a button to execute the function
- a text box where we can display the results
Next, you can begin building it!
To start creating the GUI, open up Visual Studio and create a new project.
Once Visual Studio is open, click on File (1) –> New (2) –> Project (3).
Under the New Project window, choose Visual C# (1), select WPF App (.NET Framework) (2), change the name to PoshGUI-sample (3) and click OK.
Once the project is created, a blank form will be presented with the name of MainWindow.xaml.
You now need to format this form to fit our requirements. Below are the controls and format that you’ll need to add.
- Window
- Title: Disk Information
- Height: 326
- Width: 403
- Controls (4)
- Label
- Content: “Computer Name:”
- Margin: 10, 10, 0, 0
- TextBox
- Name: txtComputer
- Text: “”
- Height: 23
- Width: 174
- Button
- Name: btnQuery
- Content: Query
- Margin: 0, 13, 12, 0
- TextBox
- Name: txtResults
- Text: “”
- IsReadOnly: True
- Margin: 10, 60, 0, 0
- Height: 225
- Width: 373
- Label
The final appearance of the form should be similar to what is shown in the image below. You can rearrange the layout of your window differently. Be creative!
Combine the Script and the GUI
Once you are happy with your design, you can now start integrating it with the script.
PowerShell cannot display forms natively. To be able to display the form, we need to add a line of code to the very top of our script to support rendering of the WPF Form.
Then add code to perform the following actions:
- Import and read the XAML code of the form.
- Dynamically create variables assigned to each named controls
- Display the form
Below is the updated code inside your script.
Note: Make sure to modify the line
$xamlFile
and point it to the full path of your MainWindow.xaml file.Note:
$Null = $window.ShowDialog()
must always be the last line of code inside your script.When you run this code by executing the Main.ps1 script, you should see the example output below.
As you can see, the three named controls were assigned their variables. These variable names will be referenced later on in the script when we add the control logic code.
- var_btnQuery
- var_btnComputer
- var_txtResults
Bear in mind that the script at this point can only display the form, but the controls are useless since you haven’t added the code yet.
Add the Button Click Event Code
Now that you’ve successfully modified the script to import and display the GUI, begin adding the code to the controls to retrieve and display the disk information data.
In this project, only the
btnQuery
button will be assigned an action. The other controls will only serve as input and output/display controls. This means that we only need to add a click event code to btnQuery
.To add the click action to
btnQuery
, assign the code below to its corresponding variable name $var_btnQuery
. Copy the code below and insert it in between the Get-Variable var_*
and $Null = $window.ShowDialog()
code references in the script.Finished Code
With all parts covered, below is the completed code for our script which incorporates the function and the GUI that we’ve designed.
Grub Editor Gui
After saving the finished script, test its functionality. Below is the example of what we expect as output from the script.
Java Gui Editor Download
Summary
In this post, you learned how to create a simple function that accepts input and return results from it. You also learned how to create a basic WPF PowerShell GUI as well as how to import it to act as a front-end for the PowerShell script you created.
This is just a basic script and GUI combination. Numerous improvements can be done such as:
- formatting the size and free space to display as GB values
- change the name of the property displayed
- use GridView instead of TextBox to show the results
- .and so on
It is up to you to modify and add functionality based on your requirements.
Further Reading
Related
The
Editor
control is used to accept multi-line input.Set and read text
The
Editor
, like other text-presenting views, exposes the Text
property. This property can be used to set and read the text presented by the Editor
. The following example demonstrates setting the Text
property in XAML:To read text, access the
Text
property in C#:Set placeholder text
The
Editor
can be set to show placeholder text when it is not storing user input. This is accomplished by setting the Placeholder
property to a string
, and is often used to indicate the type of content that is appropriate for the Editor
. In addition, the placeholder text color can be controlled by setting the PlaceholderColor
property to a Color
:Prevent text entry
Users can be prevented from modifying the text in an
Editor
by setting the IsReadOnly
property, which has a default value of false
, to true
:Note
Links golf game for mac. The
IsReadonly
property does not alter the visual appearance of an Editor
, unlike the IsEnabled
property that also changes the visual appearance of the Editor
to gray. Desktop ghost pro 1 5 download free.Transform text
An
Editor
can transform the casing of its text, stored in the Text
property, by setting the TextTransform
property to a value of the TextTransform
enumeration. This enumeration has four values:None
indicates that the text won't be transformed.Default
indicates that the default behavior for the platform will be used. This is the default value of theTextTransform
property.Lowercase
indicates that the text will be transformed to lowercase.Uppercase
indicates that the text will be transformed to uppercase.
The following example shows transforming text to uppercase:
The equivalent C# code is: Tf2 spy auto backstab hack.
Limit input length
The
MaxLength
property can be used to limit the input length that's permitted for the Editor
. This property should be set to a positive integer:A
MaxLength
property value of 0 indicates that no input will be allowed, and a value of int.MaxValue
, which is the default value for an Editor
, indicates that there is no effective limit on the number of characters that may be entered.Character spacing
Character spacing can be applied to an
Editor
by setting the Editor.CharacterSpacing
property to a double
value:The equivalent C# code is:
Form Gui Editor Patch 1
The result is that characters in the text displayed by the
Editor
are spaced CharacterSpacing
device-independent units apart.Note
The
CharacterSpacing
property value is applied to the text displayed by the Text
and Placeholder
properties.Auto-size an Editor
Form Gui Editor Patch Free
An
Editor
can be made to auto-size to its content by setting the Editor.AutoSize
property to TextChanges
, which is a value of the EditoAutoSizeOption
enumeration. This enumeration has two values:Disabled
indicates that automatic resizing is disabled, and is the default value.TextChanges
indicates that automatic resizing is enabled.
This can be accomplished in code as follows:
When auto-resizing is enabled, the height of the
Editor
will increase when the user fills it with text, and the height will decrease as the user deletes text.Note
An
Editor
will not auto-size if the HeightRequest
property has been set.Customize the keyboard
The keyboard that's presented when users interact with an
Editor
can be set programmatically via the Keyboard
property, to one of the following properties from the Keyboard
class:Chat
– used for texting and places where emoji are useful.Default
– the default keyboard.Email
– used when entering email addresses.Numeric
– used when entering numbers.Plain
– used when entering text, without anyKeyboardFlags
specified.Telephone
– used when entering telephone numbers.Text
– used when entering text.Url
– used for entering file paths & web addresses.
This can be accomplished in XAML as follows:
The equivalent C# code is:
Examples of each keyboard can be found in our Recipes repository.
The
Keyboard
class also has a Create
factory method that can be used to customize a keyboard by specifying capitalization, spellcheck, and suggestion behavior. KeyboardFlags
enumeration values are specified as arguments to the method, with a customized Keyboard
being returned. The KeyboardFlags
enumeration contains the following values:None
– no features are added to the keyboard.CapitalizeSentence
– indicates that the first letter of the first word of each entered sentence will be automatically capitalized.Spellcheck
– indicates that spellcheck will be performed on entered text.Suggestions
– indicates that word completions will be offered on entered text.CapitalizeWord
– indicates that the first letter of each word will be automatically capitalized.CapitalizeCharacter
– indicates that every character will be automatically capitalized.CapitalizeNone
– indicates that no automatic capitalization will occur.All
– indicates that spellcheck, word completions, and sentence capitalization will occur on entered text.
The following XAML code example shows how to customize the default
Keyboard
to offer word completions and capitalize every entered character:The equivalent C# code is:
Enable and disable spell checking
The
IsSpellCheckEnabled
property controls whether spell checking is enabled. By default, the property is set to true
. As the user enters text, misspellings are indicated.However, for some text entry scenarios, such as entering a username, spell checking provides a negative experience and so should be disabled by setting the
IsSpellCheckEnabled
property to false
:Note
When the
IsSpellCheckEnabled
property is set to false
, and a custom keyboard isn't being used, the native spell checker will be disabled. However, if a Keyboard
has been set that disables spell checking, such as Keyboard.Chat
, the IsSpellCheckEnabled
property is ignored. Therefore, the property cannot be used to enable spell checking for a Keyboard
that explicitly disables it.Enable and disable text prediction
The
IsTextPredictionEnabled
property controls whether text prediction and automatic text correction is enabled. By default, the property is set to true
. As the user enters text, word predictions are presented.However, for some text entry scenarios, such as entering a username, text prediction and automatic text correction provides a negative experience and should be disabled by setting the
IsTextPredictionEnabled
property to false
:Note
When the
IsTextPredictionEnabled
property is set to false
, and a custom keyboard isn't being used, text prediction and automatic text correction is disabled. However, if a Keyboard
has been set that disables text prediction, the IsTextPredictionEnabled
property is ignored. Therefore, the property cannot be used to enable text prediction for a Keyboard
that explicitly disables it.Colors
Editor
can be set to use a custom background color via the BackgroundColor
property. Special care is necessary to ensure that colors will be usable on each platform. Because each platform has different defaults for text color, you may need to set a custom background color for each platform. See Working with Platform Tweaks for more information about optimizing the UI for each platform.In C#:
In XAML:
Make sure that the background and text colors you choose are usable on each platform and don't obscure any placeholder text.
Events and interactivity
Editor
exposes two events:- TextChanged – raised when the text changes in the editor. Provides the text before and after the change.
- Completed – raised when the user has ended input by pressing the return key on the keyboard.
Note
The
VisualElement
class, from which Entry
inherits, also has Focused
and Unfocused
events.Completed
The
Completed
event is used to react to the completion of an interaction with an Editor
. Completed
is raised when the user ends input with a field by entering the return key on the keyboard (or by pressing the Tab key on UWP). The handler for the event is a generic event handler, taking the sender and EventArgs
:The completed event can be subscribed to in code and XAML:
In C#:
In XAML:
TextChanged
The
TextChanged
event is used to react to a change in the content of a field.TextChanged
is raised whenever the Text
of the Editor
changes. The handler for the event takes an instance of TextChangedEventArgs
. TextChangedEventArgs
provides access to the old and new values of the Editor
Text
via the OldTextValue
and NewTextValue
properties:How to get paint tool sai for mac. The completed event can be subscribed to in code and XAML:
In code:
In XAML: