11---
22description : The automatic variable that contains the current object in the pipeline object.
33Locale : en-US
4- ms.date : 12/07/2022
4+ ms.date : 01/31/2025
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_psitem?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about_PSItem
@@ -15,7 +15,7 @@ The automatic variable that contains the current object in the pipeline object.
1515## Long description
1616
1717PowerShell includes the ` $PSItem ` variable and its alias, ` $_ ` , as
18- [ automatic variables] [ 01 ] in scriptblocks that process the current object, such
18+ [ automatic variables] [ 03 ] in scriptblocks that process the current object, such
1919as in the pipeline. This article uses ` $PSItem ` in the examples, but ` $PSItem `
2020can be replaced with ` $_ ` in every example.
2121
@@ -24,23 +24,24 @@ a pipeline.
2424
2525There are a few common use cases for ` $PSItem ` :
2626
27- - in the scriptblock for the ** Process** parameter of the
27+ - In the scriptblock for the ** Process** parameter of the
2828 ` ForEach-Object ` cmdlet
29- - in the scriptblock for the ** FilterScript** parameter of the ` Where-Object `
29+ - In the scriptblock for the ** FilterScript** parameter of the ` Where-Object `
3030 cmdlet
31- - in the intrinsic methods ** ForEach** and ** Where**
31+ - In the intrinsic methods ** ForEach** and ** Where**
3232- with delay-bind scriptblock parameters
33- - in a ` switch ` statement's conditional values and associated scriptblocks
34- - in the ` process ` block of a function
35- - in a ` filter ` definition
36- - in the scriptblock of the ** ValidateScript** attribute
33+ - In a ` switch ` statement's conditional values and associated scriptblocks
34+ - In the ` process ` block of a function
35+ - In a ` filter ` definition
36+ - In the scriptblock of the ** ValidateScript** attribute
37+ - In the scriptblock of a ` catch ` statement
3738
3839The rest of this article includes examples of using ` $PSItem ` for these use
3940cases.
4041
4142## ForEach-Object Process
4243
43- The [ ForEach-Object] [ 02 ] cmdlet is designed to operate on objects in the
44+ The [ ForEach-Object] [ 15 ] cmdlet is designed to operate on objects in the
4445pipeline, executing the ** Process** parameter's scriptblock once for every
4546object in the pipeline.
4647
@@ -75,7 +76,7 @@ Result is: 2 3 4
7576
7677## Where-Object FilterScript
7778
78- The [ Where-Object] [ 03 ] cmdlet is designed to filter objects in the pipeline.
79+ The [ Where-Object] [ 16 ] cmdlet is designed to filter objects in the pipeline.
7980
8081You can use ` $PSItem ` in the scriptblock of the ** FilterScript** parameter,
8182which executes once for each input object in the pipeline.
9495
9596## ForEach and Where methods
9697
97- Both the [ ForEach] [ 04 ] and [ Where] [ 05 ] intrinsic methods for arrays take a
98+ Both the [ ForEach] [ 01 ] and [ Where] [ 02 ] intrinsic methods for arrays take a
9899scriptblock as an input parameter. You can use the ` $PSItem ` in those
99100scriptblocks to access the current object.
100101
@@ -111,7 +112,7 @@ current object. Then the scriptblock of the **Where** method returns only `B`.
111112
112113## Delay-bind scriptblock parameters
113114
114- [ Delay-bind scriptblocks] [ 06 ] let you use ` $PSItem ` to define parameters for a
115+ [ Delay-bind scriptblocks] [ 12 ] let you use ` $PSItem ` to define parameters for a
115116pipelined cmdlet before executing it.
116117
117118``` powershell
@@ -120,7 +121,7 @@ dir config.log | Rename-Item -NewName { "old_$($_.Name)" }
120121
121122## Switch statement scriptblocks
122123
123- In [ switch statements] [ 07 ] , you can use ` $PSItem ` in both action scriptblocks
124+ In [ switch statements] [ 13 ] , you can use ` $PSItem ` in both action scriptblocks
124125and statement condition scriptblocks.
125126
126127``` powershell
@@ -147,7 +148,7 @@ the current object is odd.
147148
148149## Function process blocks
149150
150- When you define a [ function] [ 08 ] , you can use ` $PSItem ` in the ` process ` block
151+ When you define a [ function] [ 09 ] , you can use ` $PSItem ` in the ` process ` block
151152definition but not in the ` begin ` or ` end ` block definitions. If you
152153reference ` $PSItem ` in the ` begin ` or ` end ` blocks, the value is ` $null `
153154because those blocks don't operate on each object in the pipeline.
@@ -171,10 +172,10 @@ function Add-One {
171172```
172173
173174> [ !TIP]
174- > While you can use ` $PSItem ` in [ advanced functions] [ 09 ] , there's little
175+ > While you can use ` $PSItem ` in [ advanced functions] [ 08 ] , there's little
175176> reason to do so. If you intend to receive input from the pipeline,
176177> it's best to define parameters with one of the ` ValueFromPipeline* ` arguments
177- > for the [ Parameter] [ 10 ] attribute.
178+ > for the [ Parameter] [ 06 ] attribute.
178179
179180Using the ** Parameter** attribute and cmdlet binding for advanced functions
180181makes the implementation more explicit and predictable than processing the
@@ -275,7 +276,7 @@ VERBOSE: Input object 3 is:
275276
276277## Filter definitions
277278
278- You can use ` $PSItem ` in the statement list of a [ filter] [ 11 ] 's definition.
279+ You can use ` $PSItem ` in the statement list of a [ filter] [ 10 ] 's definition.
279280
280281When you use ` $PSItem ` in a ` filter ` definition, the value is the current
281282object if the filter is called in the pipeline and otherwise ` $null ` .
@@ -297,7 +298,7 @@ is an even number and `$false` if it isn't.
297298
298299## The ValidateScript attribute scriptblock
299300
300- You can use ` $PSItem ` in the scriptblock of a [ ValidateScript] [ 15 ] attribute.
301+ You can use ` $PSItem ` in the scriptblock of a [ ValidateScript] [ 07 ] attribute.
301302When used with ** ValidateScript** , ` $PSItem ` is the value of the current object
302303being validated. When the variable or parameter value is an array, the
303304scriptblock is called once for each object in the array with ` $PSItem ` as the
@@ -353,29 +354,56 @@ value isn't even.
353354The ` Add-EvenNumber ` function adds the valid input numbers and returns the
354355total.
355356
357+ ## The catch statement scriptblock
358+
359+ Within a ` catch ` block, the current error can be accessed using ` $PSItem ` . The
360+ object is of type ** ErrorRecord** .
361+
362+ ``` powershell
363+ try { NonsenseString }
364+ catch {
365+ Write-Host "An error occurred:"
366+ Write-Host $_
367+ }
368+ ```
369+
370+ Running this script returns the following result:
371+
372+ ``` Output
373+ An Error occurred:
374+ The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
375+ script file, or operable program. Check the spelling of the name, or if a path
376+ was included, verify that the path is correct and try again.
377+ ```
378+
379+ For more examples, see the _ Accessing exception information_ section in
380+ [ about_Try_Catch_Finally] [ 14 ] .
381+
356382## See also
357383
358- - [ about_Arrays] [ 04 ]
359- - [ about_Automatic_Variables] [ 01 ]
360- - [ about_Comparison_Operators] [ 12 ]
361- - [ about_Functions] [ 08 ]
362- - [ about_Script_Blocks] [ 14 ]
363- - [ about_Switch] [ 07 ]
364- - [ ForEach-Object] [ 02 ]
365- - [ Where-Object] [ 03 ]
384+ - [ about_Arrays] [ 01 ]
385+ - [ about_Automatic_Variables] [ 03 ]
386+ - [ about_Comparison_Operators] [ 04 ]
387+ - [ about_Functions] [ 09 ]
388+ - [ about_Script_Blocks] [ 11 ]
389+ - [ about_Switch] [ 13 ]
390+ - [ ForEach-Object] [ 15 ]
391+ - [ Where-Object] [ 16 ]
366392
367393<!-- link references -->
368- [ 01 ] : about_Automatic_Variables.md
369- [ 02 ] : xref:Microsoft.PowerShell.Core.ForEach-Object
370- [ 03 ] : xref:Microsoft.PowerShell.Core.Where-Object
371- [ 04 ] : about_Arrays.md#foreach
372- [ 05 ] : about_Arrays.md#where
373- [ 06 ] : about_Script_Blocks.md#using-delay-bind-script-blocks-with-parameters
374- [ 07 ] : about_Switch.md
375- [ 08 ] : about_Functions.md
376- [ 09 ] : about_Functions_Advanced.md
377- [ 10 ] : about_Functions_Advanced_Parameters.md#parameter-attribute
378- [ 11 ] : about_Functions.md#filters
379- [ 12 ] : about_Comparison_Operators.md#replacement-operator
380- [ 14 ] : about_Script_Blocks.md
381- [ 15 ] : about_Functions_Advanced_Parameters.md#validatescript-validation-attribute
394+ [ 01 ] : about_Arrays.md#foreach
395+ [ 02 ] : about_Arrays.md#where
396+ [ 03 ] : about_Automatic_Variables.md
397+ [ 04 ] : about_Comparison_Operators.md#replacement-operator
398+
399+ [ 06 ] : about_Functions_Advanced_Parameters.md#parameter-attribute
400+ [ 07 ] : about_Functions_Advanced_Parameters.md#validatescript-validation-attribute
401+ [ 08 ] : about_Functions_Advanced.md
402+ [ 09 ] : about_Functions.md
403+ [ 10 ] : about_Functions.md#filters
404+ [ 11 ] : about_Script_Blocks.md
405+ [ 12 ] : about_Script_Blocks.md#using-delay-bind-script-blocks-with-parameters
406+ [ 13 ] : about_Switch.md
407+ [ 14 ] : about_Try_Catch_Finally.md#accessing-exception-information
408+ [ 15 ] : xref:Microsoft.PowerShell.Core.ForEach-Object
409+ [ 16 ] : xref:Microsoft.PowerShell.Core.Where-Object
0 commit comments