Skip to content

Commit

Permalink
Add example to explain side-effects (#11201)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwheeler committed Jun 20, 2024
1 parent 8cf7339 commit b413c0c
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 8 deletions.
81 changes: 80 additions & 1 deletion reference/5.1/Microsoft.PowerShell.Utility/Select-Object.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 03/16/2023
ms.date: 06/19/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: Select-Object
Expand Down Expand Up @@ -348,6 +348,81 @@ Name Weight
a 7
```

### Example 13 - ExpandProperty alters the original object

This example demonstrates the side-effect of using the **ExpandProperty** parameter. When you use
**ExpandProperty**, `Select-Object` adds the selected properties to the original object as
**NoteProperty** members.

```powershell
PS> $object = [PSCustomObject]@{
name = 'USA'
children = [PSCustomObject]@{
name = 'Southwest'
}
}
PS> $object
name children
---- --------
USA @{name=Southwest}
# Use the ExpandProperty parameter to expand the children property
PS> $object | Select-Object @{n="country"; e={$_.name}} -ExpandProperty children
name country
---- -------
Southwest USA
# The original object has been altered
PS> $object
name children
---- --------
USA @{name=Southwest; country=USA}
```

As you can see, the **country** property was added to the **children** object after using the
**ExpandProperty** parameter.

### Example 14 - Create a new object with expanded properties without altering the input object

You can avoid the side-effect of using the **ExpandProperty** parameter by creating a new object and
copying the properties from the input object.

```powershell
PS> $object = [PSCustomObject]@{
name = 'USA'
children = [PSCustomObject]@{
name = 'Southwest'
}
}
PS> $object
name children
---- --------
USA @{name=Southwest}
# Create a new object with selected properties
PS> $newobject = [PSCustomObject]@{
country = $object.name
children = $object.children
}
PS> $newobject
country children
------- --------
USA @{name=Southwest}
# $object remains unchanged
PS> $object
name children
---- --------
USA @{name=Southwest}
```

## PARAMETERS

### -ExcludeProperty
Expand Down Expand Up @@ -379,6 +454,10 @@ error.

In either case, the output objects' **Type** matches the expanded property's **Type**.

> [!NOTE]
> There is a side-effect when using **ExpandProperty**. The `Select-Object` adds the selected
> properties to the original object as **NoteProperty** members.

If the **Property** parameter is specified, `Select-Object` attempts to add each selected property
as a **NoteProperty** to every outputted object.

Expand Down
81 changes: 80 additions & 1 deletion reference/7.2/Microsoft.PowerShell.Utility/Select-Object.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 11/16/2023
ms.date: 06/19/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.2&WT.mc_id=ps-gethelp
schema: 2.0.0
title: Select-Object
Expand Down Expand Up @@ -350,6 +350,81 @@ name weight
a 7
```

### Example 13: ExpandProperty alters the original object

This example demonstrates the side-effect of using the **ExpandProperty** parameter. When you use
**ExpandProperty**, `Select-Object` adds the selected properties to the original object as
**NoteProperty** members.

```powershell
PS> $object = [PSCustomObject]@{
name = 'USA'
children = [PSCustomObject]@{
name = 'Southwest'
}
}
PS> $object
name children
---- --------
USA @{name=Southwest}
# Use the ExpandProperty parameter to expand the children property
PS> $object | Select-Object @{n="country"; e={$_.name}} -ExpandProperty children
name country
---- -------
Southwest USA
# The original object has been altered
PS> $object
name children
---- --------
USA @{name=Southwest; country=USA}
```

As you can see, the **country** property was added to the **children** object after using the
**ExpandProperty** parameter.

### Example 14: Create a new object with expanded properties without altering the input object

You can avoid the side-effect of using the **ExpandProperty** parameter by creating a new object and
copying the properties from the input object.

```powershell
PS> $object = [PSCustomObject]@{
name = 'USA'
children = [PSCustomObject]@{
name = 'Southwest'
}
}
PS> $object
name children
---- --------
USA @{name=Southwest}
# Create a new object with selected properties
PS> $newobject = [PSCustomObject]@{
country = $object.name
children = $object.children
}
PS> $newobject
country children
------- --------
USA @{name=Southwest}
# $object remains unchanged
PS> $object
name children
---- --------
USA @{name=Southwest}
```

## PARAMETERS

### -ExcludeProperty
Expand Down Expand Up @@ -383,6 +458,10 @@ error.

In either case, the output objects' **Type** matches the expanded property's **Type**.

> [!NOTE]
> There is a side-effect when using **ExpandProperty**. The `Select-Object` adds the selected
> properties to the original object as **NoteProperty** members.

If the **Property** parameter is specified, `Select-Object` attempts to add each selected property
as a **NoteProperty** to every outputted object.

Expand Down
85 changes: 82 additions & 3 deletions reference/7.4/Microsoft.PowerShell.Utility/Select-Object.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
Locale: en-US
Module Name: Microsoft.PowerShell.Utility
ms.date: 11/16/2023
ms.date: 06/19/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: Select-Object
Expand All @@ -19,8 +19,8 @@ Selects objects or object properties.

```
Select-Object [-InputObject <PSObject>] [[-Property] <Object[]>] [-ExcludeProperty <String[]>]
[-ExpandProperty <String>] [-Unique] [-CaseInsensitive] [-Last <Int32>] [-First <Int32>] [-Skip <Int32>]
[-Wait] [<CommonParameters>]
[-ExpandProperty <String>] [-Unique] [-CaseInsensitive] [-Last <Int32>] [-First <Int32>]
[-Skip <Int32>] [-Wait] [<CommonParameters>]
```

### SkipLastParameter
Expand Down Expand Up @@ -367,6 +367,81 @@ name weight
a 7
```

### Example 14: ExpandProperty alters the original object

This example demonstrates the side-effect of using the **ExpandProperty** parameter. When you use
**ExpandProperty**, `Select-Object` adds the selected properties to the original object as
**NoteProperty** members.

```powershell
PS> $object = [PSCustomObject]@{
name = 'USA'
children = [PSCustomObject]@{
name = 'Southwest'
}
}
PS> $object
name children
---- --------
USA @{name=Southwest}
# Use the ExpandProperty parameter to expand the children property
PS> $object | Select-Object @{n="country"; e={$_.name}} -ExpandProperty children
name country
---- -------
Southwest USA
# The original object has been altered
PS> $object
name children
---- --------
USA @{name=Southwest; country=USA}
```

As you can see, the **country** property was added to the **children** object after using the
**ExpandProperty** parameter.

### Example 15: Create a new object with expanded properties without altering the input object

You can avoid the side-effect of using the **ExpandProperty** parameter by creating a new object and
copying the properties from the input object.

```powershell
PS> $object = [PSCustomObject]@{
name = 'USA'
children = [PSCustomObject]@{
name = 'Southwest'
}
}
PS> $object
name children
---- --------
USA @{name=Southwest}
# Create a new object with selected properties
PS> $newobject = [PSCustomObject]@{
country = $object.name
children = $object.children
}
PS> $newobject
country children
------- --------
USA @{name=Southwest}
# $object remains unchanged
PS> $object
name children
---- --------
USA @{name=Southwest}
```

## PARAMETERS

### -CaseInsensitive
Expand Down Expand Up @@ -419,6 +494,10 @@ error.

In either case, the output objects' **Type** matches the expanded property's **Type**.

> [!NOTE]
> There is a side-effect when using **ExpandProperty**. The `Select-Object` adds the selected
> properties to the original object as **NoteProperty** members.

If the **Property** parameter is specified, `Select-Object` attempts to add each selected property
as a **NoteProperty** to every outputted object.

Expand Down
Loading

0 comments on commit b413c0c

Please sign in to comment.