1
1
---
2
2
description : Explains how to use the `powershell.exe` command-line interface. Displays the command-line parameters and describes the syntax.
3
3
Locale : en-US
4
- ms.date : 01/03 /2024
4
+ ms.date : 07/23 /2024
5
5
no-no-loc : [-Command, -ConfigurationName , -EncodedCommand, -ExecutionPolicy, -File, -Help, -InputFormat, -Mta, -NoExit, -NoLogo, -NonInteractive, -NoProfile, -OutputFormat, -PSConsoleFile, -Sta, -Version, -WindowStyle]
6
6
online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1&WT.mc_id=ps-gethelp
7
7
schema : 2.0.0
@@ -45,11 +45,71 @@ PowerShell[.exe] -Help | -? | /?
45
45
46
46
## Parameters
47
47
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 `
49
62
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.
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 ` ).
71
+
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
53
113
54
114
The value of ** Command** can be ` - ` , a script block, or a string. If the value
55
115
of ** Command** is ` - ` , the command text is read from standard input.
@@ -66,10 +126,10 @@ powershell -Command {Get-WinEvent -LogName security}
66
126
```
67
127
68
128
In ` 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.
73
133
74
134
A string passed to ** Command** is still executed as PowerShell code, so the
75
135
script 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
88
148
returned to the parent shell as deserialized XML objects, not live objects. For
89
149
other shells, the results are returned as strings.
90
150
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
92
152
input. You must redirect standard input when using the ** Command** parameter
93
153
with standard input. For example:
94
154
@@ -111,18 +171,24 @@ hi there
111
171
out
112
172
```
113
173
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.
120
-
121
- For more information, see ` $LASTEXITCODE ` in [ about_Automatic_Variables] [ 03 ] .
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.
122
179
123
- Similarly, the value 1 is returned when a script-terminating
124
- (runspace-terminating) error, such as a ` throw ` or ` -ErrorAction Stop ` , occurs
125
- or when execution is interrupted with <kbd >Ctrl</kbd >+<kbd >C</kbd >.
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
182
+ ` $false ` . If the last command is an external program or a PowerShell script
183
+ that explicitly sets an exit code other than ` 0 ` or ` 1 ` , that exit code is
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 >.
188
+
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 ] .
126
192
127
193
### -ConfigurationName \< string\>
128
194
@@ -150,65 +216,6 @@ not change the PowerShell execution policy that's set in the registry. For
150
216
information about PowerShell execution policies, including a list of valid
151
217
values, see [ about_Execution_Policies] [ 04 ] .
152
218
153
- ### -File - | \< filePath\> \< args\>
154
-
155
- If the value of ** File** is ` - ` , the command text is read from standard input.
156
- Running ` powershell -File - ` without redirected standard input starts a regular
157
- session. This is the same as not specifying the ** File** parameter at all.
158
-
159
- If the value of ** File** is a filepath, the script runs in the local scope
160
- ("dot-sourced") of the new session, so that the functions and variables that
161
- the script creates are available in that new session. Enter the script filepath
162
- and any parameters. ** File** must be the last parameter in the command. All
163
- values typed after the ** File** parameter are interpreted as the script
164
- filepath and parameters passed to that script.
165
-
166
- Parameters passed to the script are passed as literal strings, after
167
- interpretation by the current shell. For example, if you are in ** cmd.exe** and
168
- want to pass an environment variable value, you would use the ** cmd.exe**
169
- syntax: ` powershell.exe -File .\test.ps1 -TestParam %windir% `
170
-
171
- In contrast, running ` powershell.exe -File .\test.ps1 -TestParam $env:windir `
172
- in ** cmd.exe** results in the script receiving the literal string ` $env:windir `
173
- because it has no special meaning to the current ** cmd.exe** shell. The
174
- ` $env:windir ` style of environment variable reference _ can_ be used inside a
175
- ** Command** parameter, since there it will be interpreted as PowerShell code.
176
-
177
- Similarly, if you want to execute the same command from a ** Batch script** , you
178
- would use ` %~dp0 ` instead of ` .\ ` or ` $PSScriptRoot ` to represent the current
179
- execution directory: ` powershell.exe -File %~dp0test.ps1 -TestParam %windir% ` .
180
- If you instead used ` .\test.ps1 ` , PowerShell would throw an error because it
181
- can't find the literal path ` .\test.ps1 `
182
-
183
- When the value of ** File** is a filepath, ** File** _ must_ be the last parameter
184
- in the command because any characters typed after the ** File** parameter name
185
- are interpreted as the script filepath followed by the script parameters.
186
-
187
- You can include the script parameters and values in the value of the ** File**
188
- parameter. For example: ` -File .\Get-Script.ps1 -Domain Central `
189
-
190
- Typically, the switch parameters of a script are either included or omitted.
191
- For example, the following command uses the ** All** parameter of the
192
- ` Get-Script.ps1 ` script file: ` -File .\Get-Script.ps1 -All `
193
-
194
- In rare cases, you might need to provide a ** Boolean** value for a parameter.
195
- It isn't possible to pass an explicit boolean value for a switch parameter when
196
- running a script in this way. This limitation was removed in PowerShell 6
197
- (` pwsh.exe ` ).
198
-
199
- > [ !NOTE]
200
- > The ** File** parameter can't support scripts using a parameter that expects
201
- > an array of argument values. This, unfortunately, is a limitation of how a
202
- > native command gets argument values. When you call a native executable (such
203
- > as ` powershell ` or ` pwsh ` ), it doesn't know what to do with an array, so
204
- > it's passed as a string.
205
-
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 ` .
209
-
210
- For more information, see ` $LASTEXITCODE ` in [ about_Automatic_Variables] [ 03 ] .
211
-
212
219
### -InputFormat {Text | XML}
213
220
214
221
Describes the format of data sent to PowerShell. Valid values are ` Text ` (text
0 commit comments