# Select-Object (Alias: Select)

PowerShell provides powerful cmdlets like `Select-Object`, `Sort-Object`, and `Group-Object` to manipulate and organize data in pipelines. These cmdlets allow you to filter, sort, and group objects based on specific properties, making it easier to work with large datasets.

***

#### **1. Select-Object (Alias: Select)**

The `Select-Object` cmdlet is used to select specific properties or a subset of objects from a pipeline. It creates new objects with only the selected properties or objects.

**Selecting Properties**

To select specific properties of an object:

```
Get-Process "powershell*" | Select-Object -Property Name, CPU
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FoIVBZ0u25lVeYMZFfa8P%2Fimage.png?alt=media&#x26;token=a31cc6db-ebed-47c3-a17c-1ffe63be61c6" alt=""><figcaption></figcaption></figure>

**Expanding Properties**

If a property is an array (e.g., `Modules` in a process object), you can expand it to extract individual items:

```
Get-Process "powershell*" | Select-Object -ExpandProperty Modules
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2Fd2urUXG2d4YyBTCXYFuy%2Fimage.png?alt=media&#x26;token=0de1c57b-c4fa-478b-b465-600d6f0c91f7" alt=""><figcaption></figcaption></figure>

**Selecting First, Last, or Unique Items**

* **First**: Select the first `n` objects.
* **Last**: Select the last `n` objects.
* **Unique**: Select only unique items based on a property.

**Examples:**

```
# Select the first 5 services
Get-Service | Select-Object -First 5

# Select the last 10 events from the System event log
Get-EventLog -LogName System | Select-Object -Last 10

# Select unique Event IDs from the Application event log
Get-EventLog Application | Select-Object EventID -Unique
```

***

#### **2. Select-String: PowerShell's Version of GREP**

The `Select-String` cmdlet is used to search text using regular expressions, similar to `grep` in Unix or `findstr` in Windows.

**Searching Files**

To search for a pattern in files:

```
Select-String 'K5.*[efg]lex' $env:WINDIR\Inf\*.inf | Format-List Path, Line
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FM4W8jReD21cbZZTHLfXU%2Fimage.png?alt=media&#x26;token=cc90c731-db33-4873-a770-6acc18ef0167" alt=""><figcaption></figcaption></figure>

**Searching Command Output**

To search the output of a command:

```
ipconfig.exe | Select-String 'IP.*Address' -CaseSensitive
```

**Output of Select-String**

`Select-String` outputs objects with properties like:

* **Path**: The file or input stream where the match was found.
* **LineNumber**: The line number of the match.
* **Line**: The matching line of text.

**Example:**

```
Select-String 'pattern' *.log | Select-Object Path
```

**Quiet Mode**

To return `$true` if a match is found, or nothing otherwise:

```
netstat.exe -ano -p tcp | Select-String ':139' -Quiet
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2Fz31miq41R7fPOAUedQBf%2Fimage.png?alt=media&#x26;token=f94feb3f-8dec-48b8-943b-697a95529c34" alt=""><figcaption></figcaption></figure>

***

#### **3. Sort-Object (Alias: Sort)**

The `Sort-Object` cmdlet sorts objects based on one or more properties.

**Sorting by Property**

To sort objects by a specific property:

```
# Sort files by size (ascending)
Get-ChildItem C:\ | Sort-Object Length

# Sort files by size (descending)
Get-ChildItem C:\ | Sort-Object Length -Descending
```

**Sorting by Multiple Properties**

To sort by multiple properties:

```
Get-ChildItem C:\Windows\System32\*.exe, C:\Windows\System32\*.dll |
    Sort-Object Length, Extension, Name -Descending |
    Select-Object -First 20 -Property Length, FullName
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FYEEEctqEItMrHamkvEr1%2Fimage.png?alt=media&#x26;token=971af2d2-d1d1-485a-a879-6a4d9d3b3104" alt=""><figcaption></figcaption></figure>

**Named Calculated Properties**

You can create custom properties for sorting or selecting:

```
Get-ChildItem HKLM:\System\CurrentControlSet\Control |
    Sort-Object @{Expression={$_.SubkeyCount + $_.ValueCount}} |
    Select-Object Name, @{Expression={$_.SubkeyCount + $_.ValueCount}; Name="Item Count"} -Last 10
```

***

#### **4. Group-Object**

The `Group-Object` cmdlet groups objects based on one or more properties.

**Grouping by Property**

To group objects by a property:

```
# Group files by extension
Get-ChildItem C:\Windows\System32 -Recurse |
    Group-Object -Property Extension |
    Where-Object { $_.Count -gt 10 } |
    Sort-Object Count -Descending
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FjP51j0e8lbapccS2saGQ%2Fimage.png?alt=media&#x26;token=42f2d475-e366-48f2-a62e-251f7fc4e5e2" alt=""><figcaption></figcaption></figure>

**Counting Items in Groups**

To count items in each group:

```
Get-Process | Group-Object -Property Name | Sort-Object Count -Descending
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FHXE3XIrCBGU79w7COtqU%2Fimage.png?alt=media&#x26;token=454b013b-aeff-4133-b7e9-1edefb9f93bf" alt=""><figcaption></figcaption></figure>
