Powershell is a tool created to undertake users’ commands and is also used for graphical interfaces. If you want to build or create graphical devices by yourself, a Powershell GUI for scripts will satisfy your need.
This task seems difficult and hard to approach if you are a newcomer, as it involves many steps. But if you have fundamental knowledge about the technological field, this process will not challenge you at all. Our Trustguide team will offer the simplest guidelines for you to learn. Please continue scrolling down!
Basic Prerequisites
You must meet these basic requirements first before starting to build a Powershell GUI for scripts:
- Visual Studio with 2017 version or updated ones
- An editor app or code
- A computer with the software Powershell Windows 10+
How To Create A Script
To wrap a Graphical User Interface, you need to build a script first. This post will guide you step-by-step on how to make a simple Main.ps1 script. All you need to do is to perform these simple steps outlined below:
- Step 1: To query, you must come to terms with your PC or laptop name being inputted
- Step 2: Query or program your PC or laptop again. You must also store fixed data on a variable disk at this step.
- Step 3: The results will be shown on the screen and return.
This technique is just a piece of cake, so panic is not necessary. We will dive more deeply into the following article sections.
Writing The Function
We will present to you a function called “Get-FixedDisk”, which will help you query your computer and stored information sources quicker without manually providing your commands to the computer and dot-sourcing whenever you need.
Here is the function code:
Function Get-FixedDisk {
[CmdletBinding()]
# This param() block indicates the start of parameters declaration
param (
<#
This parameter accepts the name of the target computer.
It is also set to mandatory so that the function does not execute without specifying the value.
#>
[Parameter(Mandatory)]
[string]$Computer
)
<#
WMI query command, which gets the list of all logical disks and saves the results to a variable named $DiskInfo
#>
$DiskInfo = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter ‘DriveType=3’
$DiskInfo
}
Importing The Code
To perform this task, you must apply a dot symbol coupled with space like this ( . ) then continue to apply the script path. There is an instance for you:
PS “Script Path”> . .\Main.ps1
If you are not currently in the working state in the Directory, you can follow this specific path:
PS C:>. “Script Path”\Main.ps1
Now, we have already stored the script in the memory. Next we can undertake a test to assess the previously built function, which is similar to the code below (We use a Get-FixedDisk feature to make a query or active the device poshLabExc):
PS51> Get-FixedDisk -Computer poshLabExc
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 53037772800
Size : 135838822400
VolumeName : Windows
DeviceID : D:
DriveType : 3
ProviderName :
FreeSpace : 14872641536
Size : 17178750976
VolumeName : Temporary Storage
DeviceID : E:
DriveType : 3
ProviderName :
FreeSpace : 488202240
Size : 524283904
VolumeName : System Reserved
How To Build The Powershell GUI For Scripts
Design The Powershell Graphical User Interface Form
- Step 1: You need to open your computer and the “Visual Studio”.
- Step 2: Select the “File” and then choose “New” and “Project” consecutively.
https://adamtheautomator.com/wp-content/uploads/2019/08/powershell-gui1.png
- Step 3: A new pop-up window will open. You need to choose the “Visual C#” at this step before selecting the “WPF App”.
- Step 4: Adjust the name, such as “PoshGUI-Sample” and press the Enter button.
https://adamtheautomator.com/wp-content/uploads/2019/08/visual-studio.png
- Step 5: Once you complete it successfully, a blank will pop up on the platform screen with its name: MainWindow.XAML.
https://adamtheautomator.com/wp-content/uploads/2019/08/mainwindow-xaml.png
Combine The Script And The Powershell GUI
After finishing the design step, you move to the next phase of this process.
We need a series of code, as Powershell cannot show it immediately. You can consider this:
Add-Type -AssemblyName PresentationFramework
Afterward, you can follow these steps:
- Step 1: Import the XAML command and read it carefully
- Step 2: Form variables attaching to names of the controls
- Step 3: Show the form as follows:
https://adamtheautomator.com/wp-content/uploads/2019/08/example-gui.png
The Bottom Line
Hopefully, this article has informed you of all the needed steps and tips for building a Powershell GUI for scripts. If you take interest in our posts, please forward them to your family, relatives and friends. This action can greatly support us in the future!Thank you for trusting our Trustguide site as a reliable source. Have a nice day!
Leave a Reply