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,40 +417,39 @@ 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. Five number are piped into the
423- first `ForEach-Object` command, the results of which are stored in a variable
424- 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 downstream
427- ` ForEach-Object` command, which displays the current values of `$Temp` and
428- ` $PSItem ` .
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
431429# Create a variable named $Temp
432430$Temp = 8
433431Get-Variable Temp | Format-Table
434432
435- # Note that the variable just created isn't available on the
436- # pipeline when -PipelineVariable creates the same variable name
437433$InformationPreference = 'Continue'
438- Write-Information '----------------------------------------------------------- '
434+ Write-Information '-------------------------------------------------'
439435111,222,333,444,555 | ForEach-Object -PipelineVariable Temp -Begin {
440436
441- Write-Information "Upstream (Begin): Temp = '$Temp'"
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'"
442441
443442} -Process {
444443
445- Write-Information "Upstream (Process): Temp = '$Temp ', PSItem = '$PSItem '"
446- Return $PSItem
444+ Write-Information "Upstream (Process): PSItem = '$PSItem ', Temp = '$Temp '"
445+ return $PSItem
447446
448447} | ForEach-Object -Process {
449448
450- Write-Information "` t`t Downstream: Temp = '$Temp ', PSItem = '$PSItem '"
449+ Write-Information "` tDownstream: PSItem = '$PSItem ', Temp = '$Temp '"
451450
452451}
453- Write-Information '----------------------------------------------------------- '
452+ Write-Information '-------------------------------------------------'
454453
455454# The $Temp variable is deleted when the pipeline finishes
456455Get-Variable Temp | Format-Table
@@ -461,19 +460,19 @@ Name Value
461460---- -----
462461Temp 8
463462
464- -----------------------------------------------------------
465- Upstream (Begin): Temp = ''
466- Upstream (Process): Temp = '', PSItem = '111 '
467- Downstream: Temp = '111', PSItem = '111'
468- Upstream (Process): Temp = '111 ', PSItem = '222 '
469- Downstream: Temp = '222', PSItem = '222'
470- Upstream (Process): Temp = '222 ', PSItem = '333 '
471- Downstream: Temp = '333', PSItem = '333'
472- Upstream (Process): Temp = '333 ', PSItem = '444 '
473- Downstream: Temp = '444', PSItem = '444'
474- Upstream (Process): Temp = '444 ', PSItem = '555 '
475- Downstream: Temp = '555', PSItem = '555'
476- -----------------------------------------------------------
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+ -------------------------------------------------
477476
478477Name Value
479478---- -----
@@ -486,57 +485,55 @@ Temp
486485> examples, ` Get-Partition ` is a CDXML function and ` Get-CimInstance ` is a
487486> CimCmdlet.
488487
489- 1 . CDXML functions use ` [CmdletBinding()] ` , which allows the
490- ** PipelineVariable** parameter.
491-
492- ``` powershell
493- Get-Partition -pv pvar
494- ```
495-
496- However, when you use ** PipelineVariable** in Windows PowerShell v5.1, you
497- receive the following error.
498-
499- ``` Output
500- Get-Partition : Cannot retrieve the dynamic parameters for the cmdlet.
501- Object reference not set to an instance of an object.
502-
503- At line:1 char:1
504- + get-partition -PipelineVariable pvar
505- + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
506- + CategoryInfo : InvalidArgument: (:) [Get-Partition], ParameterBindingException
507- + FullyQualifiedErrorId : GetDynamicParametersException,Get-Partition
508- ```
509-
510- 1 . When the preceding command is _ not_ a CDXML command and the downstream
511- contains either command type, the ** PipelineVariable** remains as the last
512- accumulated object.
513-
514- ``` powershell
515- Get-CimInstance Win32_DiskDrive -pv pvar |
516- ForEach-Object {
517- Write-Host "Before: $($pvar.Index)"
518- [pscustomobject]@{ DiskNumber = $_.Index }
519- } |
520- Get-Partition |
521- ForEach-Object {
522- Write-Host "After: $($pvar.Index)"
523- }
524- ```
525-
526- Notice that the value of ` $pvar ` set to the last object in the pipeline for
527- the second ` ForEach-Object ` command.
528-
529- ``` Output
530- Before: 1
531- Before: 2
532- Before: 0
533- After: 0
534- After: 0
535- After: 0
536- After: 0
537- After: 0
538- After: 0
539- ```
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+ ```
540537
541538### -ProgressAction
542539
0 commit comments