With PowerShell’s abundant cmdlets, you can retrieve almost any type of system information. However, you often receive more data than you really need. To make the output on the console easier to read, PowerShell offers the Select-Object cmdlet, which allows you to only extract particular object properties.

For instance, if a query returns a file object, it usually has various properties, such as name, extension, several access timestamps, attributes, and size. Depending on the task, you’ll only need one or a few properties.

Displaying attributes with Get-Member ^

If you don’t know the object’s properties, you can easily reveal them by piping the object to the Get-Member cmdlet. You can then use Select-Object to only read the property values that you are interested in.

Select-Object has the alias Select that saves you typing at the command prompt. However, it is a best practice to avoid aliases in scripts because you can’t know for sure that the aliases exist on the target system.

For example, to determine the size of a file, you could use the following command:

Get-ChildItem -File C:\windows\notepad.exe | Select -Property Length

In this case, only the file size of notepad.exe would be shown. You may also omit the -Property parameter if you place the property names right after the Select cmdlet.

Selecting multiple properties ^

If the output of Get-ChildItem consists of multiple files or directories, a column of numbers without additional information isn’t very helpful. Say you also want to display the file name, Select-Object then enables you to specify multiple properties that you have to separate with commas:

Get-ChildItem -File C:\windows\notepad.exe | Select FullName, Length

The presentation of the output depends on the number of properties. If you entered up to four properties, the cmdlet uses the table format; otherwise, it uses the list format.

Displaying file name and size in table format

Displaying file name and size in table format

You can also choose the format yourself by piping the output to the Format-Table or Format-List cmdlet.

Excluding attributes with -ExcludeProperty ^

If you want to display most of the attributes and only exclude a few, you can use the -ExcludeProperty parameter:

Get-ChildItem -File C:\windows\notepad.exe | Select -Property * -ExcludeProperty Length, Attributes

This example demonstrates that -ExcludeProperty requires the -Property parameter to have the value “*.”

Restricting the output with -First and -Last ^

Additional switches for restricting the output are -First and -Last, -Index, and -Skip. The parameters -First and -Last expect a numerical value that specifies the number of objects from the beginning and from the end, respectively.

Get-Process | Select Name, ID -First 3

Selecting the first three processes

Selecting the first three processes

This command shows the name and ID of the first three processes that Get-Process returns. Because the output is an array, you can exclude single objects from the array’s index by using the switches -Index and -Skip:

Get-Process | Select -Index 2

Note that you can’t combine both parameters with -Property. This means that Select-Object will return all object properties in this example. Furthermore, you have to consider that -Skip starts counting the array elements at 1, whereas PowerShell usually begins with 0.

Removing redundant values with -Unique ^

Sometimes, you need to generate a list of all existing object property values that a cmdlet returns, and you therefore have to exclude values that appear multiple times. In this case, you can remove all redundant values with the -Unique parameter. The following example generates a list of all process names and ensures that processes with the same name, such as svchost or chrome, appear only once in the list:

Get-Process | Select Name -Unique
avatar
1 Comment
  1. Kim 1 year ago

    Real nice article on filtering (in any way).
    A good way to start to understand it, with easy to follow examples.
    Using the same data and go from there really show how to handle all the different options.
    Wich more was like this (and a lot of them are, BUT just TOO hard to find). Thanks (saved the link - it will help me much)

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account