Skip to content

Commit 2330fe7

Browse files
authored
Explain REPL behavior of -FIle and -Command (#11287)
1 parent 51e2dff commit 2330fe7

File tree

4 files changed

+72
-52
lines changed

4 files changed

+72
-52
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to use the `powershell.exe` command-line interface. Displays the command-line parameters and describes the syntax.
33
Locale: en-US
4-
ms.date: 01/03/2024
4+
ms.date: 07/23/2024
55
no-no-loc: [-Command, -ConfigurationName , -EncodedCommand, -ExecutionPolicy, -File, -Help, -InputFormat, -Mta, -NoExit, -NoLogo, -NonInteractive, -NoProfile, -OutputFormat, -PSConsoleFile, -Sta, -Version, -WindowStyle]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -47,9 +47,9 @@ PowerShell[.exe] -Help | -? | /?
4747

4848
### -Command
4949

50-
Executes the specified commands (and any parameters) as though they were typed
51-
at the PowerShell command prompt, and then exits, unless the `NoExit`
52-
parameter is specified.
50+
Executes the specified commands (and any parameters), one statement at a time,
51+
as though they were typed at the PowerShell command prompt, and then exits,
52+
unless the `NoExit` parameter is specified.
5353

5454
The value of **Command** can be `-`, a script block, or a string. If the value
5555
of **Command** is `-`, the command text is read from standard input.
@@ -111,12 +111,13 @@ hi there
111111
out
112112
```
113113

114-
The process exit code is determined by status of the last (executed) command
115-
within the script block. The exit code is `0` when `$?` is `$true` or `1` when
116-
`$?` is `$false`. If the last command is an external program or a PowerShell
117-
script that explicitly sets an exit code other than `0` or `1`, that exit code
118-
is converted to `1` for process exit code. To preserve the specific exit code,
119-
add `exit $LASTEXITCODE` to your command string or script block.
114+
If the input code does not parse correctly, the statement isn't executed. The
115+
process exit code is determined by status of the last (executed) command within
116+
the script block. The exit code is `0` when `$?` is `$true` or `1` when `$?` is
117+
`$false`. If the last command is an external program or a PowerShell script
118+
that explicitly sets an exit code other than `0` or `1`, that exit code is
119+
converted to `1` for process exit code. To preserve the specific exit code, add
120+
`exit $LASTEXITCODE` to your command string or script block.
120121

121122
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][03].
122123

@@ -203,9 +204,13 @@ running a script in this way. This limitation was removed in PowerShell 6
203204
> as `powershell` or `pwsh`), it doesn't know what to do with an array, so
204205
> it's passed as a string.
205206
206-
When the script file terminates with an `exit` command, the process exit code
207-
is set to the numeric argument used with the `exit` command. With normal
208-
termination, the exit code is always `0`.
207+
The statements in the file are executed, one statement at a time, as though
208+
they were typed at the PowerShell command prompt. If a statement in the file
209+
doesn't parse correctly, the statement isn't executed. The process exit code is
210+
determined by status of the last (executed) command within the script block.
211+
With normal termination, the exit code is always `0`. When the script file
212+
terminates with an `exit` command, the process exit code is set to the numeric
213+
argument used with the `exit` command.
209214

210215
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][03].
211216

reference/7.2/Microsoft.PowerShell.Core/About/about_Pwsh.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to use the `pwsh` command-line interface. Displays the command-line parameters and describes the syntax.
33
Locale: en-US
4-
ms.date: 01/10/2024
4+
ms.date: 07/23/2024
55
no-loc: [-File, -f, -Command, -c, -ConfigurationName, -config, -CustomPipeName, -EncodedCommand, -e, -ec, -ExecutionPolicy, -ex, -ep, -InputFormat, -inp, -if, -Interactive, -i, -Login, -l, -MTA, -NoExit, -noe, -NoLogo, -nol, -NonInteractive, -noni, -NoProfile, -nop, -OutputFormat, -o, -of, -SettingsFile, -settings, -SSHServerMode, -sshs, -STA, -Version, -v, -WindowStyle, -w, -WorkingDirectory, -wd, -Help]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pwsh?view=powershell-7.2&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -101,9 +101,13 @@ find the literal path `.\test.ps1`
101101
> as `powershell` or `pwsh`), it doesn't know what to do with an array, so
102102
> it's passed as a string.
103103
104-
When the script file terminates with an `exit` command, the process exit code
105-
is set to the numeric argument used with the `exit` command. With normal
106-
termination, the exit code is always `0`.
104+
The statements in the file are executed, one statement at a time, as though
105+
they were typed at the PowerShell command prompt. If a statement in the file
106+
doesn't parse correctly, the statement isn't executed. The process exit code is
107+
determined by status of the last (executed) command within the script block.
108+
With normal termination, the exit code is always `0`. When the script file
109+
terminates with an `exit` command, the process exit code is set to the numeric
110+
argument used with the `exit` command.
107111

108112
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][02].
109113

@@ -119,9 +123,9 @@ with <kbd>Ctrl</kbd>+<kbd>C</kbd> the exit code is `0`.
119123
120124
### -Command | -c
121125

122-
Executes the specified commands (and any parameters) as though they were typed
123-
at the PowerShell command prompt, and then exits, unless the `NoExit`
124-
parameter is specified.
126+
Executes the specified commands (and any parameters), one statement at a time,
127+
as though they were typed at the PowerShell command prompt, and then exits,
128+
unless the `NoExit` parameter is specified.
125129

126130
The value of **Command** can be `-`, a script block, or a string. If the value
127131
of **Command** is `-`, the command text is read from standard input.
@@ -183,12 +187,13 @@ hi there
183187
out
184188
```
185189

186-
The process exit code is determined by status of the last (executed) command
187-
within the script block. The exit code is `0` when `$?` is `$true` or `1` when
188-
`$?` is `$false`. If the last command is an external program or a PowerShell
189-
script that explicitly sets an exit code other than `0` or `1`, that exit code
190-
is converted to `1` for process exit code. To preserve the specific exit code,
191-
add `exit $LASTEXITCODE` to your command string or script block.
190+
If the input code does not parse correctly, the statement isn't executed. The
191+
process exit code is determined by status of the last (executed) command within
192+
the script block. The exit code is `0` when `$?` is `$true` or `1` when `$?` is
193+
`$false`. If the last command is an external program or a PowerShell script
194+
that explicitly sets an exit code other than `0` or `1`, that exit code is
195+
converted to `1` for process exit code. To preserve the specific exit code, add
196+
`exit $LASTEXITCODE` to your command string or script block.
192197

193198
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][02].
194199

reference/7.4/Microsoft.PowerShell.Core/About/about_Pwsh.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to use the `pwsh` command-line interface. Displays the command-line parameters and describes the syntax.
33
Locale: en-US
4-
ms.date: 07/05/2024
4+
ms.date: 07/23/2024
55
no-loc: [-File, -f, -Command, -c, -CommandWithArgs, -cwa, -ConfigurationName, -config, -CustomPipeName, -EncodedCommand, -e, -ec, -ExecutionPolicy, -ex, -ep, -InputFormat, -inp, -if, -Interactive, -i, -Login, -l, -MTA, -NoExit, -noe, -NoLogo, -nol, -NonInteractive, -noni, -NoProfile, -nop, -OutputFormat, -o, -of, -SettingsFile, -settings, -SSHServerMode, -sshs, -STA, -Version, -v, -WindowStyle, -w, -WorkingDirectory, -wd, -Help]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pwsh?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -103,9 +103,13 @@ find the literal path `.\test.ps1`
103103
> as `powershell` or `pwsh`), it doesn't know what to do with an array, so
104104
> it's passed as a string.
105105
106-
When the script file terminates with an `exit` command, the process exit code
107-
is set to the numeric argument used with the `exit` command. With normal
108-
termination, the exit code is always `0`.
106+
The statements in the file are executed, one statement at a time, as though
107+
they were typed at the PowerShell command prompt. If a statement in the file
108+
doesn't parse correctly, the statement isn't executed. The process exit code is
109+
determined by status of the last (executed) command within the script block.
110+
With normal termination, the exit code is always `0`. When the script file
111+
terminates with an `exit` command, the process exit code is set to the numeric
112+
argument used with the `exit` command.
109113

110114
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][02].
111115

@@ -121,9 +125,9 @@ with <kbd>Ctrl</kbd>+<kbd>C</kbd> the exit code is `0`.
121125
122126
### -Command | -c
123127

124-
Executes the specified commands (and any parameters) as though they were typed
125-
at the PowerShell command prompt, and then exits, unless the `NoExit`
126-
parameter is specified.
128+
Executes the specified commands (and any parameters), one statement at a time,
129+
as though they were typed at the PowerShell command prompt, and then exits,
130+
unless the `NoExit` parameter is specified.
127131

128132
The value of **Command** can be `-`, a script block, or a string. If the value
129133
of **Command** is `-`, the command text is read from standard input.
@@ -185,12 +189,13 @@ hi there
185189
out
186190
```
187191

188-
The process exit code is determined by status of the last (executed) command
189-
within the script block. The exit code is `0` when `$?` is `$true` or `1` when
190-
`$?` is `$false`. If the last command is an external program or a PowerShell
191-
script that explicitly sets an exit code other than `0` or `1`, that exit code
192-
is converted to `1` for process exit code. To preserve the specific exit code,
193-
add `exit $LASTEXITCODE` to your command string or script block.
192+
If the input code does not parse correctly, the statement isn't executed. The
193+
process exit code is determined by status of the last (executed) command within
194+
the script block. The exit code is `0` when `$?` is `$true` or `1` when `$?` is
195+
`$false`. If the last command is an external program or a PowerShell script
196+
that explicitly sets an exit code other than `0` or `1`, that exit code is
197+
converted to `1` for process exit code. To preserve the specific exit code, add
198+
`exit $LASTEXITCODE` to your command string or script block.
194199

195200
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][02].
196201

reference/7.5/Microsoft.PowerShell.Core/About/about_Pwsh.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to use the `pwsh` command-line interface. Displays the command-line parameters and describes the syntax.
33
Locale: en-US
4-
ms.date: 07/05/2024
4+
ms.date: 07/23/2024
55
no-loc: [-File, -f, -Command, -c, -CommandWithArgs, -cwa, -ConfigurationName, -config, -CustomPipeName, -EncodedCommand, -e, -ec, -ExecutionPolicy, -ex, -ep, -InputFormat, -inp, -if, -Interactive, -i, -Login, -l, -MTA, -NoExit, -noe, -NoLogo, -nol, -NonInteractive, -noni, -NoProfile, -nop, -OutputFormat, -o, -of, -SettingsFile, -settings, -SSHServerMode, -sshs, -STA, -Version, -v, -WindowStyle, -w, -WorkingDirectory, -wd, -Help]
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pwsh?view=powershell-7.5&WT.mc_id=ps-gethelp
77
schema: 2.0.0
@@ -103,9 +103,13 @@ find the literal path `.\test.ps1`
103103
> as `powershell` or `pwsh`), it doesn't know what to do with an array, so
104104
> it's passed as a string.
105105
106-
When the script file terminates with an `exit` command, the process exit code
107-
is set to the numeric argument used with the `exit` command. With normal
108-
termination, the exit code is always `0`.
106+
The statements in the file are executed, one statement at a time, as though
107+
they were typed at the PowerShell command prompt. If a statement in the file
108+
doesn't parse correctly, the statement isn't executed. The process exit code is
109+
determined by status of the last (executed) command within the script block.
110+
With normal termination, the exit code is always `0`. When the script file
111+
terminates with an `exit` command, the process exit code is set to the numeric
112+
argument used with the `exit` command.
109113

110114
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][02].
111115

@@ -121,9 +125,9 @@ with <kbd>Ctrl</kbd>+<kbd>C</kbd> the exit code is `0`.
121125
122126
### -Command | -c
123127

124-
Executes the specified commands (and any parameters) as though they were typed
125-
at the PowerShell command prompt, and then exits, unless the `NoExit`
126-
parameter is specified.
128+
Executes the specified commands (and any parameters), one statement at a time,
129+
as though they were typed at the PowerShell command prompt, and then exits,
130+
unless the `NoExit` parameter is specified.
127131

128132
The value of **Command** can be `-`, a script block, or a string. If the value
129133
of **Command** is `-`, the command text is read from standard input.
@@ -185,12 +189,13 @@ hi there
185189
out
186190
```
187191

188-
The process exit code is determined by status of the last (executed) command
189-
within the script block. The exit code is `0` when `$?` is `$true` or `1` when
190-
`$?` is `$false`. If the last command is an external program or a PowerShell
191-
script that explicitly sets an exit code other than `0` or `1`, that exit code
192-
is converted to `1` for process exit code. To preserve the specific exit code,
193-
add `exit $LASTEXITCODE` to your command string or script block.
192+
If the input code does not parse correctly, the statement isn't executed. The
193+
process exit code is determined by status of the last (executed) command within
194+
the script block. The exit code is `0` when `$?` is `$true` or `1` when `$?` is
195+
`$false`. If the last command is an external program or a PowerShell script
196+
that explicitly sets an exit code other than `0` or `1`, that exit code is
197+
converted to `1` for process exit code. To preserve the specific exit code, add
198+
`exit $LASTEXITCODE` to your command string or script block.
194199

195200
For more information, see `$LASTEXITCODE` in [about_Automatic_Variables][02].
196201

0 commit comments

Comments
 (0)