Skip to content

Commit 4a3ff85

Browse files
authored
Merge pull request #12027 from MicrosoftDocs/main
4/24/2025 PM Publish
2 parents 5a6e9bf + dad3025 commit 4a3ff85

File tree

12 files changed

+400
-89
lines changed

12 files changed

+400
-89
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_CommonParameters.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes the parameters that can be used with any cmdlet.
33
Locale: en-US
4-
ms.date: 07/02/2024
4+
ms.date: 04/24/2025
55
no-loc: [Debug, Verbose, Confirm]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -471,6 +471,64 @@ At line:1 char:1
471471
+ FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
472472
```
473473

474+
> [!CAUTION]
475+
> There are two known issues with using the **PipelineVariable** parameter in a
476+
> pipeline that includes CimCmdlets or CDXML cmdlets. In the following
477+
> examples, `Get-Partition` is a CDXML function and `Get-CimInstance` is a
478+
> CimCmdlet.
479+
480+
1. CDXML functions use `[CmdletBinding()]`, which allows the
481+
**PipelineVariable** parameter.
482+
483+
```powershell
484+
Get-Partition -pv pvar
485+
```
486+
487+
However, when you use **PipelineVariable** in Windows PowerShell v5.1, you
488+
receive the following error.
489+
490+
```Output
491+
Get-Partition : Cannot retrieve the dynamic parameters for the cmdlet.
492+
Object reference not set to an instance of an object.
493+
494+
At line:1 char:1
495+
+ get-partition -PipelineVariable pvar
496+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
497+
+ CategoryInfo : InvalidArgument: (:) [Get-Partition], ParameterBindingException
498+
+ FullyQualifiedErrorId : GetDynamicParametersException,Get-Partition
499+
```
500+
501+
1. When the preceding command is _not_ a CDXML command and the downstream
502+
contains either command type, the **PipelineVariable** remains as the last
503+
accumulated object.
504+
505+
```powershell
506+
Get-CimInstance Win32_DiskDrive -pv pvar |
507+
ForEach-Object {
508+
Write-Host "Before: $($pvar.Index)"
509+
[pscustomobject]@{ DiskNumber = $_.Index }
510+
} |
511+
Get-Partition |
512+
ForEach-Object {
513+
Write-Host "After: $($pvar.Index)"
514+
}
515+
```
516+
517+
Notice that the value of `$pvar` set to the last object in the pipeline for
518+
the second `ForEach-Object` command.
519+
520+
```Output
521+
Before: 1
522+
Before: 2
523+
Before: 0
524+
After: 0
525+
After: 0
526+
After: 0
527+
After: 0
528+
After: 0
529+
After: 0
530+
```
531+
474532
### -ProgressAction
475533

476534
Determines how PowerShell responds to progress updates generated by a script,

reference/5.1/Microsoft.PowerShell.Core/ForEach-Object.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/26/2024
5+
ms.date: 04/23/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: ForEach-Object
@@ -36,7 +36,7 @@ input objects can be piped to the cmdlet or specified using the **InputObject**
3636
Starting in Windows PowerShell 3.0, there are two different ways to construct a `ForEach-Object`
3737
command.
3838

39-
- **Script block**. You can use a script block to specify the operation. Within the script block,
39+
- **Script block syntax**. You can use a script block to specify the operation. Within the script block,
4040
use the `$_` variable to represent the current object. The script block is the value of the
4141
**Process** parameter. The script block can contain any PowerShell script.
4242

@@ -52,15 +52,18 @@ command.
5252
> The script blocks run in the caller's scope. Therefore, the blocks have access to variables in
5353
> that scope and can create new variables that persist in that scope after the cmdlet completes.
5454
55-
- **Operation statement**. You can also write an operation statement, which is much more like
56-
natural language. You can use the operation statement to specify a property value or call a
57-
method. Operation statements were introduced in Windows PowerShell 3.0.
55+
- **Simplified syntax**. Using the simplified syntax, you specify a property or method name of the
56+
object in the pipeline. `ForEach-Object` returns the value of the property or method for each
57+
object in the pipeline.
5858

5959
For example, the following command also gets the value of the **ProcessName** property of each
6060
process on the computer.
6161

6262
`Get-Process | ForEach-Object ProcessName`
6363

64+
The simplified syntax was introduced in Windows PowerShell 3.0. For more information, see
65+
[about_Simplified_Syntax](About/about_Simplified_Syntax.md).
66+
6467
## EXAMPLES
6568

6669
### Example 1: Divide integers in an array
@@ -82,8 +85,9 @@ This example takes an array of three integers and divides each one of them by 10
8285
This example processes the files and directories in the PowerShell installation directory `$PSHOME`.
8386

8487
```powershell
85-
Get-ChildItem $PSHOME |
86-
ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; " " }}
88+
Get-ChildItem $PSHOME | ForEach-Object -Process {
89+
if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; " " }
90+
}
8791
```
8892

8993
If the object isn't a directory, the script block gets the name of the file, divides the value of

reference/5.1/Microsoft.PowerShell.Core/Where-Object.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/26/2024
5+
ms.date: 04/23/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/where-object?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Where-Object
@@ -241,32 +241,38 @@ particular version of Windows.
241241
Starting in Windows PowerShell 3.0, there are two different ways to construct a `Where-Object`
242242
command.
243243

244-
- **Script block**. You can use a script block to specify the property name, a comparison operator,
244+
- **Script block syntax**. You can use a script block to specify the property name, a comparison operator,
245245
and a property value. `Where-Object` returns all objects for which the script block statement is
246246
true.
247247

248-
For example, the following command gets processes in the `Normal` priority class, that is,
249-
processes where the value of the **PriorityClass** property equals `Normal`.
248+
For example, the following command gets processes where the value of the **PriorityClass**
249+
property equals `Normal`.
250250

251251
`Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}`
252252

253253
All PowerShell comparison operators are valid in the script block format. For more information,
254254
see [about_Comparison_Operators](./About/about_Comparison_Operators.md).
255255

256-
- **Comparison statement**. You can also write a comparison statement, which is much more like
257-
natural language. Comparison statements were introduced in Windows PowerShell 3.0.
256+
- **Simplified syntax**. To enable the simiplified syntax, `Where-Object` includes 31 switch
257+
parameters that represent the comparison operators. The simplified syntax is easier to read and
258+
write than the script block syntax. You can combine one of the switch parameters with the
259+
**Property** and **Value** parameters to create a command that filters objects based on the
260+
values of their properties.
258261

259262
For example, the following commands also get processes that have a priority class of `Normal`.
260263
These commands are equivalent and you can use them interchangeably.
261264

262-
`Get-Process | Where-Object -Property PriorityClass -EQ -Value "Normal"`
265+
`Get-Process | Where-Object -Property PriorityClass -Value Normal -EQ`
263266

264-
`Get-Process | Where-Object PriorityClass -EQ "Normal"`
267+
`Get-Process | Where-Object PriorityClass -EQ Normal`
265268

266-
Starting in Windows PowerShell 3.0, `Where-Object` adds comparison operators as parameters in a
267-
`Where-Object` command. Unless specified, all operators are case-insensitive. Before Windows
268-
PowerShell 3.0, the comparison operators in the PowerShell language were only usable in script
269-
blocks.
269+
As shown in the example, the parameter names **Property** and **Value** are optional. The
270+
**Property** parameter is a positional parameter mapped to position `0`. The **Value** parameter
271+
is a positional parameter mapped to position `1`. The switch parameter, used to specify the
272+
comparison, can be used in any position.
273+
274+
The simplfied syntax was introduced in Windows PowerShell 3.0. For more information, see
275+
[about_Simplified_Syntax](About/about_Simplified_Syntax.md).
270276

271277
When you provide a single **Property** to `Where-Object`, the cmdlet treats the value of the
272278
property as a boolean expression. When the value of the property's **Length** isn't zero, the
@@ -1181,4 +1187,4 @@ You can read more about these methods here [about_Arrays](./About/about_Arrays.m
11811187

11821188
[Tee-Object](../Microsoft.PowerShell.Utility/Tee-Object.md)
11831189

1184-
[about_Booleans](about/about_Booleans.md)
1190+
[about_Booleans](./About/about_Booleans.md)

reference/7.4/Microsoft.PowerShell.Core/About/about_CommonParameters.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes the parameters that can be used with any cmdlet.
33
Locale: en-US
4-
ms.date: 07/02/2024
4+
ms.date: 04/24/2025
55
no-loc: [Debug, Verbose, Confirm]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -459,6 +459,77 @@ Name Value
459459
temp
460460
```
461461

462+
> [!CAUTION]
463+
> There are two known issues with using the **PipelineVariable** parameter in a
464+
> pipeline that includes CimCmdlets or CDXML cmdlets. In the following
465+
> examples, `Get-Partition` is a CDXML function and `Get-CimInstance` is a
466+
> CimCmdlet.
467+
468+
1. When the first command is a CDXML function and downstream contains either a
469+
CimCmdlet cmdlet or CDXML function, **PipelineVariable** is reset to
470+
`$null`.
471+
472+
```powershell
473+
Get-Partition -pv pvar |
474+
ForEach-Object {
475+
Write-Host "Before: $($pvar.PartitionNumber)"
476+
[pscustomobject]@{Filter = "Index = $($_.DiskNumber)"}
477+
} |
478+
Get-CimInstance Win32_DiskDrive |
479+
ForEach-Object {
480+
Write-Host "After: $($pvar.PartitionNumber)"
481+
}
482+
```
483+
484+
Notice that the value of `$pvar` set to `$null` in the pipeline for the
485+
second `ForEach-Object` command.
486+
487+
```Output
488+
Before: 1
489+
Before: 1
490+
Before: 2
491+
Before: 3
492+
Before: 4
493+
Before: 1
494+
After:
495+
After:
496+
After:
497+
After:
498+
After:
499+
After:
500+
```
501+
502+
1. When the preceding command is _not_ a CDXML command and the downstream
503+
contains either command type, the **PipelineVariable** remains as the last
504+
accumulated object.
505+
506+
```powershell
507+
Get-CimInstance Win32_DiskDrive -pv pvar |
508+
ForEach-Object {
509+
Write-Host "Before: $($pvar.Index)"
510+
[pscustomobject]@{ DiskNumber = $_.Index }
511+
} |
512+
Get-Partition |
513+
ForEach-Object {
514+
Write-Host "After: $($pvar.Index)"
515+
}
516+
```
517+
518+
Notice that the value of `$pvar` set to the last object in the pipeline for
519+
the second `ForEach-Object` command.
520+
521+
```Output
522+
Before: 1
523+
Before: 2
524+
Before: 0
525+
After: 0
526+
After: 0
527+
After: 0
528+
After: 0
529+
After: 0
530+
After: 0
531+
```
532+
462533
### -ProgressAction
463534

464535
Determines how PowerShell responds to progress updates generated by a script,

reference/7.4/Microsoft.PowerShell.Core/ForEach-Object.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/13/2025
5+
ms.date: 04/23/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: ForEach-Object
@@ -45,7 +45,7 @@ input objects can be piped to the cmdlet or specified using the **InputObject**
4545
Starting in Windows PowerShell 3.0, there are two different ways to construct a `ForEach-Object`
4646
command.
4747

48-
- **Script block**. You can use a script block to specify the operation. Within the script block,
48+
- **Script block syntax**. You can use a script block to specify the operation. Within the script block,
4949
use the `$_` variable to represent the current object. The script block is the value of the
5050
**Process** parameter. The script block can contain any PowerShell script.
5151

@@ -61,15 +61,18 @@ command.
6161
> The script blocks run in the caller's scope. Therefore, the blocks have access to variables in
6262
> that scope and can create new variables that persist in that scope after the cmdlet completes.
6363
64-
- **Operation statement**. You can also write an operation statement, which is much more like
65-
natural language. You can use the operation statement to specify a property value or call a
66-
method. Operation statements were introduced in Windows PowerShell 3.0.
64+
- **Simplified syntax**. Using the simplified syntax, you specify a property or method name of the
65+
object in the pipeline. `ForEach-Object` returns the value of the property or method for each
66+
object in the pipeline.
6767

6868
For example, the following command also gets the value of the **ProcessName** property of each
6969
process on the computer.
7070

7171
`Get-Process | ForEach-Object ProcessName`
7272

73+
The simplified syntax was introduced in Windows PowerShell 3.0. For more information, see
74+
[about_Simplified_Syntax](About/about_Simplified_Syntax.md).
75+
7376
- **Parallel running script block**. Beginning with PowerShell 7.0, a third parameter set is
7477
available that runs each script block in parallel. The **ThrottleLimit** parameter limits the
7578
number of parallel scripts running at a time. As before, use the `$_` variable to represent the
@@ -109,8 +112,9 @@ This example takes an array of three integers and divides each one of them by 10
109112
This example processes the files and directories in the PowerShell installation directory `$PSHOME`.
110113

111114
```powershell
112-
Get-ChildItem $PSHOME |
113-
ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; " " }}
115+
Get-ChildItem $PSHOME | ForEach-Object -Process {
116+
if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; " " }
117+
}
114118
```
115119

116120
If the object isn't a directory, the script block gets the name of the file, divides the value of

reference/7.4/Microsoft.PowerShell.Core/Where-Object.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 04/26/2024
5+
ms.date: 04/23/2025
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Where-Object
@@ -248,32 +248,38 @@ particular version of Windows.
248248
Starting in Windows PowerShell 3.0, there are two different ways to construct a `Where-Object`
249249
command.
250250

251-
- **Script block**. You can use a script block to specify the property name, a comparison operator,
251+
- **Script block syntax**. You can use a script block to specify the property name, a comparison operator,
252252
and a property value. `Where-Object` returns all objects for which the script block statement is
253253
true.
254254

255-
For example, the following command gets processes in the `Normal` priority class, that is,
256-
processes where the value of the **PriorityClass** property equals `Normal`.
255+
For example, the following command gets processes where the value of the **PriorityClass**
256+
property equals `Normal`.
257257

258258
`Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}`
259259

260260
All PowerShell comparison operators are valid in the script block format. For more information,
261261
see [about_Comparison_Operators](./About/about_Comparison_Operators.md).
262262

263-
- **Comparison statement**. You can also write a comparison statement, which is much more like
264-
natural language. Comparison statements were introduced in Windows PowerShell 3.0.
263+
- **Simplified syntax**. To enable the simiplified syntax, `Where-Object` includes 31 switch
264+
parameters that represent the comparison operators. The simplified syntax is easier to read and
265+
write than the script block syntax. You can combine one of the switch parameters with the
266+
**Property** and **Value** parameters to create a command that filters objects based on the
267+
values of their properties.
265268

266269
For example, the following commands also get processes that have a priority class of `Normal`.
267270
These commands are equivalent and you can use them interchangeably.
268271

269-
`Get-Process | Where-Object -Property PriorityClass -EQ -Value "Normal"`
272+
`Get-Process | Where-Object -Property PriorityClass -Value Normal -EQ`
270273

271-
`Get-Process | Where-Object PriorityClass -EQ "Normal"`
274+
`Get-Process | Where-Object PriorityClass -EQ Normal`
272275

273-
Starting in Windows PowerShell 3.0, `Where-Object` adds comparison operators as parameters in a
274-
`Where-Object` command. Unless specified, all operators are case-insensitive. Before Windows
275-
PowerShell 3.0, the comparison operators in the PowerShell language were only usable in script
276-
blocks.
276+
As shown in the example, the parameter names **Property** and **Value** are optional. The
277+
**Property** parameter is a positional parameter mapped to position `0`. The **Value** parameter
278+
is a positional parameter mapped to position `1`. The switch parameter, used to specify the
279+
comparison, can be used in any position.
280+
281+
The simplfied syntax was introduced in Windows PowerShell 3.0. For more information, see
282+
[about_Simplified_Syntax](About/about_Simplified_Syntax.md).
277283

278284
When you provide a single **Property** to `Where-Object`, the cmdlet treats the value of the
279285
property as a boolean expression. When the value of the property's **Length** isn't zero, the
@@ -1208,4 +1214,4 @@ You can read more about these methods here [about_Arrays](./About/about_Arrays.m
12081214

12091215
[Tee-Object](../Microsoft.PowerShell.Utility/Tee-Object.md)
12101216

1211-
[about_Booleans](about/about_Booleans.md)
1217+
[about_Booleans](./About/about_Booleans.md)

0 commit comments

Comments
 (0)