Powershell

PowerShell Select-Object

From many command-line utility commands, one is Select-Object. It is widely used in PowerShell to get the specific property values of any object. We will be having some examples of using the Select-Object command utility to get the different properties of an object in the PowerShell today. Make sure to have the PowerShell configured on your Linux terminal. Open your Linux terminal first with the help of a shortcut key “Ctrl+Alt+T”. After that, open the installed PowerShell using the “pwsh” instruction in the shell as demonstrated in the image.

Example 1

Let’s start by taking a simple example of the Select-object cmdlet. We have to take information regarding processes currently active or running in our Ubuntu 20.04 system. So, within PowerShell, we have started the command with the “Get-Process” keyword. It is used to get the required mentioned object, i.e., process.

The Select-Object cmdlet has been used after the Get-Process to select the particular object property. We have used the property parameter to mention the property object we wanted to get the information about. Let say we require the information regarding all the processes by using the object “ProcessName” as a property attribute. Upon executing this command on the PowerShell, we have got the names of all the currently active and running processes in our Ubuntu 20.04 system as per the output.

The list of processes’ names will be very long and might not be likable to every user. Let’s resolve this issue by getting fewer lines of names of processes with the same Select-Object cmdlet. We will be using the same syntax of the Get-Process command followed by the Select-Object cmdlet. A slight change will be added here. We will add another parameter name, “First” with its numerical value to get the fewer lines of processes’ names. So, we have tried it below to get only the first 5 lines of processes that are currently working on our system. As a return, we have got only 5 first process names.

Example 2

To get the last names of currently working processes from the list, you must change the parameter “First” to “Last”. You can add any number integer to get the number of names you want to be displayed on the PowerShell.

This was all about getting the names of processes using the Select-Object cmdlet.

Example 3

Let’s use the Select-Object to get the IDs of all the processes along with their names in the Get-Process command. So, we have added the property name “ID” after “ProcessName” to get the last 5 records of processes. In return, we have got the information, i.e., ID, name, regarding the last 5 currently active processes in our PowerShell output screen shown below.

Example 4

Let’s check for another property for the object, process, i.e., WorkingSet. So, we have been utilizing the same Select-Object to get three properties of the process object, i.e., Process ID, ProcessName, and Working Set. The parameter “Last” has been used to get these mentioned properties from the last 5 lines of the list processes currently present in the system. As a result, we have got all the information regarding the mentioned properties of Processes.

We have used the updated Select-Object cmdlet command. We are not using the property ProcessName here. The data for the last 3 processes will be fetched through the Select-Object cmdlet used in the Get-Process command. In return, we have got three results as below.

Example 5

If you find it a little troublesome to handle a huge long list of processes information, you can manage it. Here comes the parameter “Skip” to avoid a bunch of long lines from the list of columns. So, we have been utilizing this parameter, i.e., “Skip” to avoid the first 245 lines of processes information and display on the left ones from the property ProcessName. As a result, only 6 records will be left in the Process list. These processes’ names will be displayed on the PowerShell display as below.

Example 6

Let’s change the attributes this time within the Select-Object cmdlet command. So, we have replaced the simple parameter “Property” with the “ExpandProperty” attribute. This will be used to expand the mentioned object. So, we have been using the object “Modules” to be expanded here with the help of a Select-Object cmdlet. We have also used the parameter “ErrorAction”, in case an error occurs within the execution, it will continue to execute. Only the first 3 lines of information from the object Module are being displayed below.

Example 7

There is another amazing parameter found and used in the “Select-Object” cmdlet command within the PowerShell. This parameter is called as “Unique” parameter. It is used to get the unique values from the list of mentioned values, i.e., integers, characters, or strings. So, within PowerShell, we have used a list of characters, i.e., alphabets and numbers displayed in the inverted commas. Then the Select-Object cmdlet has been started with only a single parameter named “Unique”. As the list of values contains the repeated values, the Unique parameter will only choose 1 from the same ones and ignore the rest. As a result, we have got only 4 values in returns; not a single one has been repeated.

Example 8

Let’s take a glance at another illustration. So we have used the 4 same string values in the Select-Object cmdlet. Upon applying the Unique parameter, we have got a single word, “Linux” in return. This is because the Unique parameter takes only 1 instance of similar values.

The parameter “First” and “Last” can also be utilized along with the Unique parameter. So, the Select-Object contains the “First” parameter at the first line of code while choosing the first 2 values to be checked, ignoring the last two. In return got the 1 unique value, i.e., “Linux”. The second line of code uses the parameter “Last” to check only the last 2 values from the mentioned ones. In return, we got a single value, “ego” as a unique value ignoring the rest.

Conclusion

This article contains the implementation of the Select-Object cmdlet within the PowerShell of Ubuntu 20.04 system, along with some attributes. We have added 8 different sample illustrations to explain its usage. We hope it will help you a lot in learning.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.