@@ -45,11 +45,71 @@ PowerShell[.exe] -Help | -? | /?
4545
4646## Parameters
4747
48- ### -Command
48+ All parameters are case-insensitive.
49+
50+ ### -File - | \< filePath\> \< args\>
51+
52+ The value of ** File** can be ` - ` or a filepath and optional parameters. If the
53+ value of ** File** is ` - ` , then commands are read from standard input.
54+
55+ If the value of ** File** is a filepath, the script runs in the local scope
56+ ("dot-sourced") of the new session, so that the functions and variables that
57+ the script creates are available in that new session. Enter the script filepath
58+ and any parameters. ** File** _ must_ be the last parameter in the command. All
59+ values typed after the ** File** parameter are interpreted as the script
60+ filepath and parameters passed to that script. For example:
61+ ` -File .\Get-Script.ps1 -Domain Central `
62+
63+ Typically, the switch parameters of a script are either included or omitted.
64+ For example, the following command uses the ** All** parameter of the
65+ ` Get-Script.ps1 ` script file: ` -File .\Get-Script.ps1 -All `
66+
67+ In rare cases, you might need to provide a ** Boolean** value for a parameter.
68+ It isn't possible to pass an explicit boolean value for a switch parameter when
69+ running a script in this way. This limitation was removed in PowerShell 6
70+ (` pwsh.exe ` ).
4971
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.
72+ Parameters passed to the script are passed as literal strings, after
73+ interpretation by the current shell. For example, if you are in ` cmd.exe ` and
74+ want to pass an environment variable value, you would use the ` cmd.exe ` syntax:
75+ ` powershell.exe -File .\test.ps1 -TestParam %windir% `
76+
77+ In contrast, running ` powershell.exe -File .\test.ps1 -TestParam $env:windir `
78+ in ` cmd.exe ` results in the script receiving the literal string ` $env:windir `
79+ because it has no special meaning to the current ` cmd.exe ` shell. The
80+ ` $env:windir ` style of environment variable reference _ can_ be used inside a
81+ ** Command** parameter, since there it's interpreted as PowerShell code.
82+
83+ Similarly, if you want to execute the same command from a _ Batch script_ , you
84+ would use ` %~dp0 ` instead of ` .\ ` or ` $PSScriptRoot ` to represent the current
85+ execution directory: ` pwsh -File %~dp0test.ps1 -TestParam %windir% ` . If you use
86+ ` .\test.ps1 ` instead, PowerShell throws an error because it can't find the
87+ literal path ` .\test.ps1 `
88+
89+ > [ !NOTE]
90+ > The ** File** parameter can't support scripts using a parameter that expects
91+ > an array of argument values. This, unfortunately, is a limitation of how a
92+ > native command gets argument values. When you call a native executable (such
93+ > as ` powershell ` or ` pwsh ` ), it doesn't know what to do with an array, so
94+ > it's passed as a string.
95+
96+ If the value of ** File** is ` - ` , then commands are read from standard input.
97+ Running ` powershell -File - ` without redirected standard input starts a regular
98+ session. This is the same as not specifying the ` File ` parameter at all. When
99+ reading from standard input, the input statements are executed one statement at
100+ a time as though they were typed at the PowerShell command prompt. If a
101+ statement doesn't parse correctly, the statement isn't executed. The process
102+ exit code is determined by status of the last (executed) command. With
103+ successful execution, the exit code is always ` 0 ` . When the script file
104+ terminates with an ` exit ` command, the process exit code is set to the numeric
105+ argument used with the ` exit ` command.
106+
107+ Similar to ` -Command ` , when a script-terminating error occurs, the exit code is
108+ set to ` 1 ` . However, unlike with ` -Command ` , when the execution is interrupted
109+ with <kbd >Ctrl</kbd >+<kbd >C</kbd > the exit code is ` 0 ` . For more information,
110+ see ` $LASTEXITCODE ` in [ about_Automatic_Variables] [ 03 ] .
111+
112+ ### -Command
53113
54114The value of ** Command** can be ` - ` , a script block, or a string. If the value
55115of ** Command** is ` - ` , the command text is read from standard input.
@@ -66,10 +126,10 @@ powershell -Command {Get-WinEvent -LogName security}
66126```
67127
68128In ` cmd.exe ` , there is no such thing as a script block (or ** ScriptBlock**
69- type), so the value passed to ** Command** will _ always_ be a string. You can
70- write a script block inside the string, but instead of being executed it will
71- behave exactly as though you typed it at a typical PowerShell prompt, printing
72- the contents of the script block back out to you.
129+ type), so the value passed to ** Command** is _ always_ a string. You can write a
130+ script block inside the string, but instead of being executed it behaves
131+ exactly as though you typed it at a typical PowerShell prompt, printing the
132+ contents of the script block back out to you.
73133
74134A string passed to ** Command** is still executed as PowerShell code, so the
75135script block curly braces are often not required in the first place when
@@ -88,7 +148,7 @@ When called from within an existing PowerShell session, the results are
88148returned to the parent shell as deserialized XML objects, not live objects. For
89149other shells, the results are returned as strings.
90150
91- If the value of ** Command** is ` - ` , the command text is read from standard
151+ If the value of ** Command** is ` - ` , the commands are read from standard
92152input. You must redirect standard input when using the ** Command** parameter
93153with standard input. For example:
94154
@@ -111,19 +171,24 @@ hi there
111171out
112172```
113173
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
174+ When reading from standard input, the input is parsed and executed one
175+ statement at a time, as though they were typed at the PowerShell command
176+ prompt. If the input code doesn't parse correctly, the statement isn't
177+ executed. Unless you use the ` -NoExit ` parameter, the PowerShell session exits
178+ when there is no more input to read from standard input.
179+
180+ The process exit code is determined by status of the last (executed) command
181+ within the input. The exit code is ` 0 ` when ` $? ` is ` $true ` or ` 1 ` when ` $? ` is
117182` $false ` . If the last command is an external program or a PowerShell script
118183that 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.
184+ converted to ` 1 ` for process exit code. Similarly, the value 1 is returned when
185+ a script-terminating (runspace-terminating) error, such as a ` throw ` or
186+ ` -ErrorAction Stop ` , occurs or when execution is interrupted with
187+ <kbd >Ctrl</kbd >+<kbd >C</kbd >.
121188
122- For more information, see ` $LASTEXITCODE ` in [ about_Automatic_Variables] [ 03 ] .
123-
124- Similarly, the value 1 is returned when a script-terminating
125- (runspace-terminating) error, such as a ` throw ` or ` -ErrorAction Stop ` , occurs
126- or when execution is interrupted with <kbd >Ctrl</kbd >+<kbd >C</kbd >.
189+ To preserve the specific exit code, add ` exit $LASTEXITCODE ` to your command
190+ string or script block. For more information, see ` $LASTEXITCODE ` in
191+ [ about_Automatic_Variables] [ 02 ] .
127192
128193### -ConfigurationName \< string\>
129194
@@ -151,69 +216,6 @@ not change the PowerShell execution policy that's set in the registry. For
151216information about PowerShell execution policies, including a list of valid
152217values, see [ about_Execution_Policies] [ 04 ] .
153218
154- ### -File - | \< filePath\> \< args\>
155-
156- If the value of ** File** is ` - ` , the command text is read from standard input.
157- Running ` powershell -File - ` without redirected standard input starts a regular
158- session. This is the same as not specifying the ** File** parameter at all.
159-
160- If the value of ** File** is a filepath, the script runs in the local scope
161- ("dot-sourced") of the new session, so that the functions and variables that
162- the script creates are available in that new session. Enter the script filepath
163- and any parameters. ** File** must be the last parameter in the command. All
164- values typed after the ** File** parameter are interpreted as the script
165- filepath and parameters passed to that script.
166-
167- Parameters passed to the script are passed as literal strings, after
168- interpretation by the current shell. For example, if you are in ** cmd.exe** and
169- want to pass an environment variable value, you would use the ** cmd.exe**
170- syntax: ` powershell.exe -File .\test.ps1 -TestParam %windir% `
171-
172- In contrast, running ` powershell.exe -File .\test.ps1 -TestParam $env:windir `
173- in ** cmd.exe** results in the script receiving the literal string ` $env:windir `
174- because it has no special meaning to the current ** cmd.exe** shell. The
175- ` $env:windir ` style of environment variable reference _ can_ be used inside a
176- ** Command** parameter, since there it will be interpreted as PowerShell code.
177-
178- Similarly, if you want to execute the same command from a ** Batch script** , you
179- would use ` %~dp0 ` instead of ` .\ ` or ` $PSScriptRoot ` to represent the current
180- execution directory: ` powershell.exe -File %~dp0test.ps1 -TestParam %windir% ` .
181- If you instead used ` .\test.ps1 ` , PowerShell would throw an error because it
182- can't find the literal path ` .\test.ps1 `
183-
184- When the value of ** File** is a filepath, ** File** _ must_ be the last parameter
185- in the command because any characters typed after the ** File** parameter name
186- are interpreted as the script filepath followed by the script parameters.
187-
188- You can include the script parameters and values in the value of the ** File**
189- parameter. For example: ` -File .\Get-Script.ps1 -Domain Central `
190-
191- Typically, the switch parameters of a script are either included or omitted.
192- For example, the following command uses the ** All** parameter of the
193- ` Get-Script.ps1 ` script file: ` -File .\Get-Script.ps1 -All `
194-
195- In rare cases, you might need to provide a ** Boolean** value for a parameter.
196- It isn't possible to pass an explicit boolean value for a switch parameter when
197- running a script in this way. This limitation was removed in PowerShell 6
198- (` pwsh.exe ` ).
199-
200- > [ !NOTE]
201- > The ** File** parameter can't support scripts using a parameter that expects
202- > an array of argument values. This, unfortunately, is a limitation of how a
203- > native command gets argument values. When you call a native executable (such
204- > as ` powershell ` or ` pwsh ` ), it doesn't know what to do with an array, so
205- > it's passed as a string.
206-
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.
214-
215- For more information, see ` $LASTEXITCODE ` in [ about_Automatic_Variables] [ 03 ] .
216-
217219### -InputFormat {Text | XML}
218220
219221Describes the format of data sent to PowerShell. Valid values are ` Text ` (text
0 commit comments