11---
22description : Describes the parameters that can be used with any cmdlet.
33Locale : en-US
4- ms.date : 09/02 /2025
4+ ms.date : 09/29 /2025
55no-loc : [Debug, Verbose, Confirm]
66online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-5.1&WT.mc_id=ps-gethelp
77schema : 2.0.0
@@ -417,57 +417,66 @@ Valid values are strings, the same as for any variable names.
417417> always contains the final piped item from the preceding command when used in
418418> a command after the blocking command.
419419
420- The following is an example of how `PipelineVariable` works. In this example,
421- the `PipelineVariable` parameter is added to a `ForEach-Object` command to
422- store the results of the command in variables. A range of numbers, 1 to 5, are
423- piped into the first `ForEach-Object` command, the results of which are stored
424- in a variable named `$temp`.
420+ The following example illustrates how the `PipelineVariable` works. In this
421+ example, five numbers are piped into the first `ForEach-Object` command. Each
422+ item in the pipeline is stored in the pipeline variable named `$Temp`.
425423
426- The results of the first `ForEach-Object` command are piped into a second
427- ` ForEach-Object` command, which displays the current values of `$temp` and
428- ` $_ ` .
424+ The `Process` block of the first `ForEach-Object` command pipes the pipeline
425+ item into the downstream `ForEach-Object` command. The state of the variables
426+ is displayed in each step .
429427
430428` ` ` powershell
431- # Create a variable named $temp
432- $temp=8
433- Get-Variable temp
434- # Note that the variable just created isn't available on the
435- # pipeline when -PipelineVariable creates the same variable name
436- 1..5 | ForEach-Object -PipelineVariable temp -Begin {
437- Write-Host "Step1[BEGIN]:` $temp=$temp"
429+ # Create a variable named $Temp
430+ $Temp = 8
431+ Get-Variable Temp | Format-Table
432+
433+ $InformationPreference = 'Continue'
434+ Write-Information '-------------------------------------------------'
435+ 111,222,333,444,555 | ForEach-Object -PipelineVariable Temp -Begin {
436+
437+ # Note that the newly create $Temp variable doesn't contain the value 8
438+ # assigned before the pipeline started and that $PSItem is empty in
439+ # the Begin block.
440+ Write-Information "Upstream (Begin): PSItem = '$PSItem', Temp = '$Temp'"
441+
438442} -Process {
439- Write-Host "Step1[PROCESS]:`$temp=$temp - `$_=$_"
440- Write-Output $_
441- } | ForEach-Object {
442- Write-Host "`tStep2[PROCESS]:`$temp=$temp - `$_=$_"
443+
444+ Write-Information "Upstream (Process): PSItem = '$PSItem', Temp = '$Temp'"
445+ return $PSItem
446+
447+ } | ForEach-Object -Process {
448+
449+ Write-Information "` tDownstream: PSItem = '$PSItem', Temp = '$Temp'"
450+
443451}
444- # The $temp variable is deleted when the pipeline finishes
445- Get-Variable temp
452+ Write-Information '-------------------------------------------------'
453+
454+ # The $Temp variable is deleted when the pipeline finishes
455+ Get-Variable Temp | Format-Table
446456```
447457
448458``` Output
449459Name Value
450460---- -----
451- temp 8
452-
453- Step1[BEGIN]:$temp=
454- Step1[PROCESS]:$temp= - $_=1
455- Step2[PROCESS]:$temp=1 - $_=1
456- Step1[PROCESS]:$temp=1 - $_=2
457- Step2[PROCESS]:$temp=2 - $_=2
458- Step1[PROCESS]:$temp=2 - $_=3
459- Step2[PROCESS]:$temp=3 - $_=3
460- Step1[PROCESS]:$temp=3 - $_=4
461- Step2[PROCESS]:$temp=4 - $_=4
462- Step1[PROCESS]:$temp=4 - $_=5
463- Step2[PROCESS]:$temp=5 - $_=5
464-
465- Get-Variable : Cannot find a variable with the name 'temp'.
466- At line:1 char:1
467- + Get-Variable temp
468- + ~~~~~~~~~~~~~~~~~
469- + CategoryInfo : ObjectNotFound: (temp:String) [Get-Variable], ItemNotFoundException
470- + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
461+ Temp 8
462+
463+ -------------------------------------------------
464+ Upstream (Begin): PSItem = '', Temp = ''
465+ Upstream (Process): PSItem = '111', Temp = ''
466+ Downstream: PSItem = '111', Temp = '111'
467+ Upstream (Process): PSItem = '222', Temp = '111'
468+ Downstream: PSItem = '222', Temp = '222'
469+ Upstream (Process): PSItem = '333', Temp = '222'
470+ Downstream: PSItem = '333', Temp = '333'
471+ Upstream (Process): PSItem = '444', Temp = '333'
472+ Downstream: PSItem = '444', Temp = '444'
473+ Upstream (Process): PSItem = '555', Temp = '444'
474+ Downstream: PSItem = '555', Temp = '555'
475+ -------------------------------------------------
476+
477+ Name Value
478+ ---- -----
479+ Temp
471480```
472481
473482> [ !CAUTION]
@@ -476,57 +485,55 @@ At line:1 char:1
476485> examples, ` Get-Partition ` is a CDXML function and ` Get-CimInstance ` is a
477486> CimCmdlet.
478487
479- 1 . CDXML functions use ` [CmdletBinding()] ` , which allows the
480- ** PipelineVariable** parameter.
481-
482- ``` powershell
483- Get-Partition -pv pvar
484- ```
485-
486- However, when you use ** PipelineVariable** in Windows PowerShell v5.1, you
487- receive the following error.
488-
489- ``` Output
490- Get-Partition : Cannot retrieve the dynamic parameters for the cmdlet.
491- Object reference not set to an instance of an object.
492-
493- At line:1 char:1
494- + get-partition -PipelineVariable pvar
495- + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
496- + CategoryInfo : InvalidArgument: (:) [Get-Partition], ParameterBindingException
497- + FullyQualifiedErrorId : GetDynamicParametersException,Get-Partition
498- ```
499-
500- 1 . When the preceding command is _ not_ a CDXML command and the downstream
501- contains either command type, the ** PipelineVariable** remains as the last
502- accumulated object.
503-
504- ``` powershell
505- Get-CimInstance Win32_DiskDrive -pv pvar |
506- ForEach-Object {
507- Write-Host "Before: $($pvar.Index)"
508- [pscustomobject]@{ DiskNumber = $_.Index }
509- } |
510- Get-Partition |
511- ForEach-Object {
512- Write-Host "After: $($pvar.Index)"
513- }
514- ```
515-
516- Notice that the value of ` $pvar ` set to the last object in the pipeline for
517- the second ` ForEach-Object ` command.
518-
519- ``` Output
520- Before: 1
521- Before: 2
522- Before: 0
523- After: 0
524- After: 0
525- After: 0
526- After: 0
527- After: 0
528- After: 0
529- ```
488+ ** Issue 1** : CDXML functions use ` [CmdletBinding()] ` , which allows the
489+ ** PipelineVariable** parameter.
490+
491+ ``` powershell
492+ Get-Partition -pv pvar
493+ ```
494+
495+ However, when you use ** PipelineVariable** in Windows PowerShell v5.1, you
496+ receive the following error.
497+
498+ ``` Output
499+ Get-Partition : Cannot retrieve the dynamic parameters for the cmdlet.
500+ Object reference not set to an instance of an object.
501+
502+ At line:1 char:1
503+ + get-partition -PipelineVariable pvar
504+ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
505+ + CategoryInfo : InvalidArgument: (:) [Get-Partition], ParameterBindingException
506+ + FullyQualifiedErrorId : GetDynamicParametersException,Get-Partition
507+ ```
508+
509+ ** Issue 2** : When the preceding command is _ not_ a CDXML command and the
510+ downstream contains either command type, the ** PipelineVariable** remains as
511+ the last accumulated object.
512+
513+ ``` powershell
514+ Get-CimInstance Win32_DiskDrive -pv pvar |
515+ ForEach-Object {
516+ Write-Host "Upstream: Disk $($pvar.Index)"
517+ return [pscustomobject]@{ DiskNumber = $_.Index }
518+ } | Get-Partition | ForEach-Object {
519+ Write-Host "Downstream: Disk $($pvar.Index)"
520+ }
521+ ```
522+
523+ Notice that the value of ` $pvar ` set to the last object in the pipeline for
524+ the second ` ForEach-Object ` command.
525+
526+ ``` Output
527+ Upstream: Disk 1
528+ Upstream: Disk 2
529+ Upstream: Disk 0
530+ Downstream: Disk 0
531+ Downstream: Disk 0
532+ Downstream: Disk 0
533+ Downstream: Disk 0
534+ Downstream: Disk 0
535+ Downstream: Disk 0
536+ ```
530537
531538### -ProgressAction
532539
0 commit comments