Sort and Select Objects in the Pipeline
Learn On Demand
Learn On Demand Pro Series

Time
45 minutes
Difficulty
Beginner

This IT Pro Challenge virtual lab shows learners how to use the Microsoft Azure Portal and Windows PowerShell to sort and select objects in the pipeline. Learners will be exposed to a number of PowerShell commands to sort objects by name, status, and filetype in any order, select objects, and sort and select objects.

Start your free 3-day trial and become one of the 3 million Cybersecurity and IT professionals advancing their career goals

Sign up with
Or

Already have an account? Sign In »

Overview

In this IT Pro Challenge, learners will understand how to use the Windows PowerShell Sort-Object, and Select-Object pipeline commands to sort and select objects by name, status, filetype, and more in both ascending and descending order. Then, learners will combine Sort-Object and Select-Object pipeline commands to retrieve and display a variety of sorted data objects in ascending and descending order. The skills acquired in this lab are useful for a career as a network or system administrator.

Overview

The scenario for this lab is that you are a system administrator, and your company needs to retrieve information from a large number of servers. To prepare for this request, your job is to figure out how to use object sorting and object selection commands on just a single server by using Windows PowerShell to retrieve this information to either the console or a file.

Configure your environment

To begin the lab, you first need to configure your environment. You will start by launching Windows PowerShell as an administrator, use the New-Item -Path “C:\” -Name “Class” -ItemType “Directory” command to create a folder (C:\Class), and then navigate to that folder.

Sort objects

Now you are going to learn various ways to sort objects in PowerShell. To start, you will run the Get-Service | Sort-Object -Property Status, Name command to display Service objects that are sorted first by Status and then by Name. Then you will run the __dir C:\Windows\System32*.exe | Sort-Object -Property Length -Descending __ command to display all of the .exe files in the C:\Windows\System32 folder, sorted by size in descending order. Next, you will run the __Get-Process | Sort-Object -Property WS -Descending __ command to display process objects by working set in descending order. Finally, you want to see the roles and features sorted by the installed property in descending order and then by Name in ascending order. To do this, you need to run the __Get-WindowsFeature | Sort-Object -Property @{Expression="Installed";Descending=$True},@{Expression="Name";Descending=$false} __command.

Select objects

Now, you will use PowerShell and use the Select-Object pipeline command (Get-Process | Select-Object -Property WS, PID, Name | Export-Csv C:\Class\ProcList.csv) to retrieve a list of all processes, return only the WS (working set), PID (process ID), and Name properties, and then send the output to a CSV file named C:\Class\ProcList.csv. To see the last ten lines of the C:\Windows\SetupAct.log file, run the Get-Content C:\windows\setupact.log | Select-Object -Last 10 command. Run the following command to extract only the string value of the Name property of all service objects, and then send the output to a text file named C:\Class\ServiceNames.txt: Get-Service | Select-Object -ExpandProperty Name.

Now you want to see the Name, PasswordExpires, and PasswordLastSet properties for users on the local computer, replace PasswordExpires with a custom property named Expiration, and then replace PasswordLastSet with a custom property named LastReset. Run the _Get-LocalUser | Select-Object -Property Name,@{Label="Expiration";Expression={$.PasswordExpires}},@{Label="LastReset";Expression={$_.PasswordLastSet}} __command.

Finally, you want to display volume information with only the DriveLetter, DriveType, Size and SizeRemaining properties selected, convert the Size and SizeRemaining values to gigabytes with 2 decimals, and then change the SizeRemaining property to a custom property named FreeSpace. Run the Get-Volume | Select-Object DriveLetter,DriveType,@{Label="Size";Expression={"{0:N2}" -f ($.Size / 1GB)}},@{Label="FreeSpace";Expression={"{0:N2}" -f ($.SizeRemaining / 1GB)}} command.

Sort and select objects together

In this final step, you will use Windows PowerShell to run a series of commands that sort and select objects together. Combining multiple pipeline commands is unique to Windows PowerShell.

  1. Output all services to a CSV file named C:\Class\AllServices.csv by running the Get-Service | Export-Csv -Path C:\Class\AllServices.csv command.
  2. Import the .csv file by running the Import-Csv -Path C:\Class\AllServices.csv command.
  3. Import the .csv file again, sort the services by status, then by name, and only return the Status, Name, and DisplayName properties by running the Import-Csv C:\Class\AllServices.csv | Sort-Object -Property Status, Name | Select-Object -Property Status,Name,DisplayName command.
  4. Display a list of the top 10 processes consuming the largest amount of working set memory by running the Get-Process | Sort-Object -Property WS -Descending | Select-Object -First 10 command.
  5. Run the following command to retrieve the TimeGenerated, EntryType, Source, InstanceID, and Message properties of the application event log, sort the results first by Instance ID in ascending order, and then by TimeGenerated in descending order, and then use a pipeline command to send the output to a CSV file named C:\Scripts\AppEvents.csv: Get-EventLog -LogName Application | Sort-Object -Property @{Expression="InstanceID";Descending=$false},@{Expression="TimeGenerated";Descending=$true} | Select-Object -Property TimeGenerated,EntryType,Source,InstanceID,Message | Export-Csv -Path C:\Class\AppEvents.csv.
  6. Retrieve the TimeGenerated, EntryType, Source, InstanceID, and Message properties of the application event log, sort the results first by Instance ID in ascending order, and then by TimeGenerated in descending order, use a pipeline command to format the output as a table that is autosized and wrapped, and then use a pipeline command to send the results to a report file named C:\Class\AppEvents.txt. Run the Get-EventLog -LogName Application | Sort-Object -Property @{Expression="InstanceID";Descending=$false},@{Expression="TimeGenerated";Descending=$true} | Select-Object -Property TimeGenerated,EntryType,Source,InstanceID,Message | Format-Table -AutoSize -Wrap | Out-File -FilePath C:\Class\AppEvents.txt command.

Summary Conclusion

By taking this virtual lab, you will learn how to sort objects, select objects, as well as sort and select objects together using Windows PowerShell.