Thursday, November 18, 2021

thumbnail

Selecting, sorting, and measuring objects

Lesson 2: Selecting, sorting, and measuring objects



In this lesson, you will learn to manipulate objects in the pipeline by using commands that sort, select, and measure objects. Selecting, sorting, and measuring objects is essential to successfully creating automation in Windows PowerShell.

Lesson objectives

After completing this lesson, you will be able to:

Explain how to sort objects by a specified property.

Sort objects by using the Sort-Object command.

Explain how to measure objects’ numeric properties.

Measure objects by using the Measure-Object command.

Explain how to display a subset of objects in a collection.

Explain how to display a customized list of objects’ properties.

Select objects by using the Select-Object command.

Explain how to create calculated properties.

Create custom calculated properties for display.

Sorting objects by a property





Some Windows PowerShell commands produce their output in a specific order. For example, the Get-Process and Get-Service commands produce output that is sorted alphabetically by nameGet-EventLog produces output that is sorted by time. In other cases, the output may not appear to be sorted at all. Sometimes, you may want command output sorted differently from the default. The Sort-Object command, which has the alias sort, can do that for you.

Sort-Object

The Sort-Object command accepts one or more property names to sort by. By default, the command sorts in ascending order. If you want to reverse the sort order, add the -Descending parameter. If you specify more than one property, the command first sorts by the first property, then by the second property, and so on. It is not possible in a single command to sort by one property in ascending order and another in descending order.

The following commands are all examples of sorting:

Get-Service | Sort-Object –Property Name –Descending
Get-Service | Sort Name –Desc
Get-Service | Sort Status,Name

By default, string properties are sorted without regard to case. That is, lowercase and uppercase letters are treated the same. The parameters of Sort-Object allow you to specify a case-sensitive sort, a specific culture’s sorting rules, and other options. As with other commands, you can view the help for Sort-Object for details and examples.

Grouping objects by property

Sorting objects also allows you to display objects in groups. The Format-List, Format-Table, and Format-Wide formatting cmdlets all have a -GroupBy parameter that accepts a property name. By using the -GroupBy parameter, you can group the output by the specified property. For example, the following command displays the names of services running on the local computer in two two-column lists that are grouped by the Status property:

Get-Service | Sort-Object Status,Name | fw -GroupBy Status


The -GroupBy parameter works similarly to the Group-Object command. The Group-Object command accepts piped input and gives you more control over the grouping of the objects. Group-Object has the alias group.

Monday, November 15, 2021

thumbnail

Working with Windows PowerShell pipeline | How to Format Pipeline Output

Formatting pipeline output









Windows PowerShell provides several ways to control the formatting of pipeline output. The default formatting of the output depends on the objects that exist in the output and the configuration files that define the output. After Windows PowerShell decides on the appropriate format, it passes the output to a set of formatting cmdlets without your input.

The formatting cmdlets are:

Format-List

Format-Table

Format-Wide

Format-Custom

You can override the default output formatting by specifying any of the preceding cmdlets as part of the pipeline.

Note: The Format-Custom cmdlet requires creating custom XML configuration files that define the format. It is used infrequently and is beyond the scope of this course.

Each formatting cmdlet accepts the -Property parameter. The -Property parameter accepts a comma-separated list of property names, and it then filters the list of properties that display and the order in which they appear. Keep in mind that when you specify property names for this parameter, those properties must have been returned by the original command—that is, the command that passed its output to the formatting cmdlet.

For example, the Get-ADUser cmdlet returns only a subset of properties, unless you specify its -Properties parameter. Therefore, if you specify the City property in the -Property parameter for a formatting cmdlet, it will appear as if the property is not set, unless you make sure that the City property is one of the properties returned for the users queried.

Some cmdlets default to passing a different set of properties for each formatting cmdlet. For example, the Get-Service cmdlet displays three properties (StatusName, and DisplayName) in a table format by default. If you display the output of Get-Service as a list by using the Get-Service | Format-List command, six additional properties will display.

Format-List

The Format-List cmdlet, as the name suggests, formats the output of a command as a simple list of properties, where each property appears on a new line. If the command passing output to Format-List returns multiple objects, a separate list of properties for each object displays. List formatting is particularly useful when a command returns a large number of properties that would be hard to read in table format.

Note: The alias for the Format-List cmdlet is fl.

To display a simple list in the console of the default properties for the processes running on the local computer, type the following command, and then press Enter:

Get-Process | Format-List 

Format-Table

The Format-Table cmdlet formats output as a table, where each row represents an object, and each column represents a propertyThe table format is useful for displaying properties of many objects at the same time and comparing the properties of those objects.

By default, the table includes the property names as the column headers, which are separated from the data by a row of dashes. The formatting of the table depends on the returned objects. You can modify this formatting by using a variety of parameters, such as:

- AutoSize. Adjusts the size and number of columns based on the width of the data. In Windows PowerShell 5.0 and newer, -AutoSize is set to true by default. In older versions of Windows PowerShell, the default values might truncate data in the table.

- HideTableHeaders. Removes the table headers from the output.

- Wrap. Causes text that is wider than the column width to wrap to the next line.

Note: The alias for the Format-Table cmdlet is ft.

To display the NameObjectClass, and Description properties for all Windows Server Active Directory objects as a table, with the columns set to automatically size and wrap the text, type the following command in the console, and then press Enter:

Get-ADObject -filter * -Properties * | ft -Property Name, ObjectClass, Description -AutoSize -Wrap

Format-Wide

The output of the Format-Wide cmdlet is a single property in a single list displayed in multiple columns. This cmdlet functions like the /w parameter of the dir command in cmd.exe. The wide format is useful for displaying large lists of simple data, such as the names of files or processes, in a compact format.

By default, Format-Wide displays its output in two columns. You can modify the number of columns by using the -Column parameter. The -AutoSize parameter, which works the same way it does for Format-Table, is also available. You cannot use -AutoSize and -Column together, however. The -Property parameter is also available, but in the case of Format-Wide, it can accept only one property name.

Note: The alias for the Format-Wide cmdlet is fw.

To send the DisplayName property of all the Group Policy Objects (GPOs) in the current domain as output in three columns, type the following command in the console, and then press Enter:

Get-GPO -all | fw -Property DisplayName -Column 3.


Powered by Blogger.