|
1 | 1 | ---
|
2 | 2 | description: Variables that customize the behavior of PowerShell.
|
3 | 3 | Locale: en-US
|
4 |
| -ms.date: 09/29/2023 |
| 4 | +ms.date: 11/30/2023 |
5 | 5 | online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.3&WT.mc_id=ps-gethelp
|
6 | 6 | schema: 2.0.0
|
7 | 7 | title: about Preference Variables
|
@@ -1065,16 +1065,20 @@ information other than errors. In these cases, you can temporarily disable the
|
1065 | 1065 | behavior and prevent non-zero exit codes from issuing errors.
|
1066 | 1066 |
|
1067 | 1067 | ```powershell
|
1068 |
| -$definedPreference = $PSNativeCommandUseErrorActionPreference |
1069 |
| -$PSNativeCommandUseErrorActionPreference = $false |
1070 |
| -robocopy.exe D:\reports\operational "\\reporting\ops" CY2022Q4.md |
1071 |
| -$robocopyExitCode = $LASTEXITCODE |
1072 |
| -if ($robocopyExitCode -gt 8) { |
1073 |
| - throw "robocopy failed with exit code $robocopyExitCode" |
| 1068 | +& { |
| 1069 | + # Disable $PSNativeCommandUseErrorActionPreference for this scriptblock |
| 1070 | + $PSNativeCommandUseErrorActionPreference = $false |
| 1071 | + robocopy.exe D:\reports\operational "\\reporting\ops" CY2022Q4.md |
| 1072 | + if ($LASTEXITCODE -gt 8) { |
| 1073 | + throw "robocopy failed with exit code $LASTEXITCODE" |
| 1074 | + } |
1074 | 1075 | }
|
1075 |
| -$PSNativeCommandUseErrorActionPreference = $definedPreference |
1076 | 1076 | ```
|
1077 | 1077 |
|
| 1078 | +In this example, the `$PSNativeCommandUseErrorActionPreference` variable is |
| 1079 | +changed inside a scriptblock. The change is local to the scriptblock. When the |
| 1080 | +scriptblock exits, the variable reverts to its previous value. |
| 1081 | + |
1078 | 1082 | ## $PSSessionApplicationName
|
1079 | 1083 |
|
1080 | 1084 | Specifies the default application name for a remote command that uses Web
|
|
0 commit comments