Skip to content

Commit b679a57

Browse files
committed
Add catch example
1 parent b6d08f9 commit b679a57

File tree

4 files changed

+305
-176
lines changed

4 files changed

+305
-176
lines changed

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

Lines changed: 89 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: The automatic variable that contains the current object in the pipeline object.
33
Locale: en-US
4-
ms.date: 12/07/2022
4+
ms.date: 01/31/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_psitem?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_PSItem
@@ -15,7 +15,7 @@ The automatic variable that contains the current object in the pipeline object.
1515
## Long description
1616

1717
PowerShell 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
1919
as in the pipeline. This article uses `$PSItem` in the examples, but `$PSItem`
2020
can be replaced with `$_` in every example.
2121

@@ -24,23 +24,25 @@ a pipeline.
2424

2525
There 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
38+
- In the substitution operand scriptblock of the `-replace` operator
3739

3840
The rest of this article includes examples of using `$PSItem` for these use
3941
cases.
4042

4143
## ForEach-Object Process
4244

43-
The [ForEach-Object][02] cmdlet is designed to operate on objects in the
45+
The [ForEach-Object][15] cmdlet is designed to operate on objects in the
4446
pipeline, executing the **Process** parameter's scriptblock once for every
4547
object in the pipeline.
4648

@@ -75,7 +77,7 @@ Result is: 2 3 4
7577

7678
## Where-Object FilterScript
7779

78-
The [Where-Object][03] cmdlet is designed to filter objects in the pipeline.
80+
The [Where-Object][16] cmdlet is designed to filter objects in the pipeline.
7981

8082
You can use `$PSItem` in the scriptblock of the **FilterScript** parameter,
8183
which executes once for each input object in the pipeline.
@@ -94,7 +96,7 @@ list.
9496

9597
## ForEach and Where methods
9698

97-
Both the [ForEach][04] and [Where][05] intrinsic methods for arrays take a
99+
Both the [ForEach][01] and [Where][02] intrinsic methods for arrays take a
98100
scriptblock as an input parameter. You can use the `$PSItem` in those
99101
scriptblocks to access the current object.
100102

@@ -111,7 +113,7 @@ current object. Then the scriptblock of the **Where** method returns only `B`.
111113

112114
## Delay-bind scriptblock parameters
113115

114-
[Delay-bind scriptblocks][06] let you use `$PSItem` to define parameters for a
116+
[Delay-bind scriptblocks][12] let you use `$PSItem` to define parameters for a
115117
pipelined cmdlet before executing it.
116118

117119
```powershell
@@ -120,7 +122,7 @@ dir config.log | Rename-Item -NewName { "old_$($_.Name)" }
120122

121123
## Switch statement scriptblocks
122124

123-
In [switch statements][07], you can use `$PSItem` in both action scriptblocks
125+
In [switch statements][13], you can use `$PSItem` in both action scriptblocks
124126
and statement condition scriptblocks.
125127

126128
```powershell
@@ -147,7 +149,7 @@ the current object is odd.
147149

148150
## Function process blocks
149151

150-
When you define a [function][08], you can use `$PSItem` in the `process` block
152+
When you define a [function][09], you can use `$PSItem` in the `process` block
151153
definition but not in the `begin` or `end` block definitions. If you
152154
reference `$PSItem` in the `begin` or `end` blocks, the value is `$null`
153155
because those blocks don't operate on each object in the pipeline.
@@ -171,10 +173,10 @@ function Add-One {
171173
```
172174

173175
> [!TIP]
174-
> While you can use `$PSItem` in [advanced functions][09], there's little
176+
> While you can use `$PSItem` in [advanced functions][08], there's little
175177
> reason to do so. If you intend to receive input from the pipeline,
176178
> it's best to define parameters with one of the `ValueFromPipeline*` arguments
177-
> for the [Parameter][10] attribute.
179+
> for the [Parameter][06] attribute.
178180
179181
Using the **Parameter** attribute and cmdlet binding for advanced functions
180182
makes the implementation more explicit and predictable than processing the
@@ -275,7 +277,7 @@ VERBOSE: Input object 3 is:
275277

276278
## Filter definitions
277279

278-
You can use `$PSItem` in the statement list of a [filter][11]'s definition.
280+
You can use `$PSItem` in the statement list of a [filter][10]'s definition.
279281

280282
When you use `$PSItem` in a `filter` definition, the value is the current
281283
object if the filter is called in the pipeline and otherwise `$null`.
@@ -297,7 +299,7 @@ is an even number and `$false` if it isn't.
297299

298300
## The ValidateScript attribute scriptblock
299301

300-
You can use `$PSItem` in the scriptblock of a [ValidateScript][15] attribute.
302+
You can use `$PSItem` in the scriptblock of a [ValidateScript][07] attribute.
301303
When used with **ValidateScript**, `$PSItem` is the value of the current object
302304
being validated. When the variable or parameter value is an array, the
303305
scriptblock is called once for each object in the array with `$PSItem` as the
@@ -353,29 +355,75 @@ value isn't even.
353355
The `Add-EvenNumber` function adds the valid input numbers and returns the
354356
total.
355357

358+
## The catch statement scriptblock
359+
360+
Within a `catch` block, the current error can be accessed using `$PSItem`. The
361+
object is of type **ErrorRecord**.
362+
363+
```powershell
364+
try { NonsenseString }
365+
catch {
366+
Write-Host "An error occurred:"
367+
Write-Host $_
368+
}
369+
```
370+
371+
Running this script returns the following result:
372+
373+
```Output
374+
An Error occurred:
375+
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
376+
script file, or operable program. Check the spelling of the name, or if a path
377+
was included, verify that the path is correct and try again.
378+
```
379+
380+
For more examples, see the _Accessing exception information_ section in
381+
[about_Try_Catch_Finally][14].
382+
383+
## The -replace operator's substitution scriptblock
384+
385+
Starting in PowerShell 6, you can use `$PSItem` when calling the [replace][04]
386+
operator and defining a [substitution scriptblock][05]. When you do, the value
387+
of `$PSItem` is the value of the current match.
388+
389+
```powershell
390+
$datePattern = '\d{4}-\d{2}-\d{2}'
391+
'Today is 1999-12-31' -replace $datePattern, { [datetime]$PSItem.Value }
392+
```
393+
394+
```output
395+
Today is 12/31/1999 00:00:00
396+
```
397+
398+
In this example, the substitution scriptblock replaces the original date string
399+
with the default format for the current culture by casting the value to
400+
**datetime**.
401+
356402
## See also
357403

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]
404+
- [about_Arrays][01]
405+
- [about_Automatic_Variables][03]
406+
- [about_Comparison_Operators][04]
407+
- [about_Functions][09]
408+
- [about_Script_Blocks][11]
409+
- [about_Switch][13]
410+
- [ForEach-Object][15]
411+
- [Where-Object][16]
366412

367413
<!-- 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
414+
[01]: about_Arrays.md#foreach
415+
[02]: about_Arrays.md#where
416+
[03]: about_Automatic_Variables.md
417+
[04]: about_Comparison_Operators.md#replacement-operator
418+
[05]: about_Comparison_Operators.md#replacement-with-a-script-block
419+
[06]: about_Functions_Advanced_Parameters.md#parameter-attribute
420+
[07]: about_Functions_Advanced_Parameters.md#validatescript-validation-attribute
421+
[08]: about_Functions_Advanced.md
422+
[09]: about_Functions.md
423+
[10]: about_Functions.md#filters
424+
[11]: about_Script_Blocks.md
425+
[12]: about_Script_Blocks.md#using-delay-bind-script-blocks-with-parameters
426+
[13]: about_Switch.md
427+
[14]: about_Try_Catch_Finally.md#accessing-exception-information
428+
[15]: xref:Microsoft.PowerShell.Core.ForEach-Object
429+
[16]: xref:Microsoft.PowerShell.Core.Where-Object

0 commit comments

Comments
 (0)