Skip to content

Basic Set Operations

A set is a collection of items which can be anything. Whatever operator we need to work on these sets are in short the set operators and the operation is also known as set operation. Basic set operation includes Union, Intersection as well as addition, subtraction, etc.

Filter an enumeration by using a conditional expression

Synonyms:

Terminal window
Where-Object
where
?

Example:

Terminal window
$names = @( "Aaron", "Albert", "Alphonse","Bernie", "Charlie", "Danny", "Ernie", "Frank")
$names | Where-Object { $_ -like "A*" }
$names | where { $_ -like "A*" }
$names | ? { $_ -like "A*" }

Returns:

Aaron
Albert
Alphonse

Sort an enumeration in either ascending or descending order

Synonyms:

Terminal window
Sort-Object
sort

Assuming:

Terminal window
$names = @( "Aaron", "Aaron", "Bernie", "Charlie", "Danny" )

Ascending sort is the default:

Terminal window
$names | Sort-Object
$names | sort

Aaron
Aaron
Bernie
Charlie
Danny

To request descending order:

Terminal window
$names | Sort-Object -Descending
$names | sort -Descending

Danny
Charlie
Bernie
Aaron
Aaron

You can sort using an expression.

Terminal window
$names | Sort-Object { $_.length }

Aaron
Aaron
Danny
Bernie
Charlie

You can group an enumeration based on an expression.

Synonyms:

Terminal window
Group-Object
group

Examples:

Terminal window
$names = @( "Aaron", "Albert", "Alphonse","Bernie", "Charlie", "Danny", "Ernie", "Frank")
$names | Group-Object -Property Length
$names | group -Property Length

Response:

|Count|Name|Group |---|---|---|---|---|---|---|---|---|--- |4|5|{Aaron, Danny, Ernie, Frank} |2|6|{Albert, Bernie} |1|8|{Alphonse} |1|7|{Charlie}

Projecting an enumeration allows you to extract specific members of each object, to extract all the details, or to compute values for each object

Synonyms:

Terminal window
Select-Object
select

Selecting a subset of the properties:

Terminal window
$dir = dir "C:\MyFolder"
$dir | Select-Object Name, FullName, Attributes
$dir | select Name, FullName, Attributes

|Name|FullName|Attributes |---|---|---|---|---|---|---|---|---|--- |Images|C:\MyFolder\Images|Directory |data.txt|C:\MyFolder\data.txt|Archive |source.c|C:\MyFolder\source.c|Archive

Selecting the first element, and show all its properties:

Terminal window
$d | select -first 1 *

| |---|---|---|---|---|---|---|---|---|--- |PSPath |PSParentPath |PSChildName |PSDrive |PSProvider |PSIsContainer |BaseName |Mode |Name |Parent |Exists |Root |FullName |Extension |CreationTime |CreationTimeUtc |LastAccessTime |LastAccessTimeUtc |LastWriteTime |LastWriteTimeUtc |Attributes

  • Group-Object
  • Group-Object -Property
  • Group-Object -Property ,
  • Group-Object -Property -CaseSensitive
  • Group-Object -Property -Culture
  • Group-Object -Property
  • Sort-Object
  • Sort-Object -Property
  • Sort-Object -Property
  • Sort-Object -Property ,
  • Sort-Object -Property -CaseSensitive
  • Sort-Object -Property -Descending
  • Sort-Object -Property -Unique
  • Sort-Object -Property -Culture