Blog ❯ Author: Fabio Moschini|Date: 09.12.2022
PowerShell examples - 2
Powershell

PowerShell Script Examples - 2
Displaying a Message Box
With PowerShell, it's possible to display information through windows, for example using a message box.With the following example, we'll display a simple message box with only a confirmation button:Output:

Let's look at the two instructions used in detail. The first one:is used to add the PresentationFramework assembly to the PowerShell session.
Within the assembly, the namespace System.Windows is defined, which contains the implementation of the MessageBox class.
The second instruction:invokes the Show method of theSystem.Windows.MessageBox class with the appropriate parameters:
- the first parameter is a string that defines the text to be displayed inside the message box
- the second parameter is a string with the title of the message box
- the third parameter is a string that indicates the buttons to display in the message box:
- - 'OK'
- - 'OKCancel'
- - 'YesNoCancel'
- - 'YesNo'
- finally, the fourth parameter is a string that defines the type of icon to display inside the message box:
- 'None'
- 'Error'
- 'Question'
- 'Warning'
- 'Information'
For message boxes that display multiple buttons, you can store the type of button pressed like this:
Changing the Displayed Text Color
To write a warning message, we use the Write-Warning cmdlet:The text is displayed in yellow.To modify the color of the displayed text as desired, use theWrite-Host instruction by setting the-BackgroundColor and-ForegroundColor parameters:Output:

Restarting the Computer
To restart the computer, you can use the Restart-Computer cmdlet:Listing Running Processes
To list the processes running on the computer, use:It's possible to sort the listed processes, for example by descending memory usage:If there are many processes, it might be useful to limit the display to the first n elements:Using Hexadecimal or Binary Numbers
To convert a hexadecimal number to decimal, simply prefix the hexadecimal number with the sequence 0x:Output:2623
To convert a number from decimal to binary, you can use:The first parameter is the decimal number to convert.
Using Custom Forms
With PowerShell, you can define "WinForm" objects by setting the appropriate properties.Forms can contain visual objects like buttons, text boxes, and labels.
Let's see an example:Let's analyze each line:
line 1: as we've seen in one of the previous examples, this instruction adds the System.Windows.Forms assembly to the PowerShell session;
line 2: creation of a Windows.Forms.Form object;
line 3: setting the form's Text property;
line 4: setting the form's dimensions;
line 5: defining the position where the form will be displayed;
line 6: creation of a System.Windows.Forms.Button object;
line 7: adding the click event handler, whose code displays the current date and time;
line 8: setting the button's Text property;
line 9: defines the button's position relative to the form;
line 10: adds the button to the form;
line 11: displays the form as a modal window. The form is very simple: it contains a button whose press displays the current date and time in the prompt from which the script is run.
Clearing the History
To clear the command history of a PowerShell session, the command to use is:Information About Disks Connected to the Computer
To get information about disks connected to the computer, use theGet-Disk cmdlet:Get-Disk lists logical disks; to get the list of physical disks, use:Finally, to get the list of volumes:Taking a Screenshot
With this script, you can take a screenshot of the main monitor:Customizing PowerShell Colors
To modify the colors in a PowerShell window, you can use this script:In this case, blue is used for the background color, while the text is white.Stopping a Process
We've already seen the Get-Process cmdlet for listing running processes.To stop a process, use the Stop-Process command followed by the parameter -Id <process id> or the parameter -Name <process name>.
The process id is information that can be determined throughGet-Process.
Examples:
- to terminate all instances of the "CalculatorApp" process:Stop-Process -Name "CalculatorApp"
- to terminate a specific instance of a process, you need to use its Id:Stop-Process -id 12345
Converting to HTML
It's possible to convert PowerShell command output information to HTML format.For example, to have an HTML page with the list of running processes, you can use this command:Let's see in detail what happens:
- the first command executed is Get-Process which returns the list of active processes;
- the result is sent to the ConvertTo-Html command through |, which extracts the information specified by-Property and uses it to create an HTML table with the title indicated by the -Title parameter;
- finally, the result of the ConvertTo-Html command is sent to Out-File to write theproc.html file.