1
1
---
2
- ms.date : 11/30/2017
2
+ ms.date : 10/04/2019
3
3
schema : 2.0.0
4
4
locale : en-us
5
5
keywords : powershell,cmdlet
6
6
title : about_Prompts
7
7
---
8
8
# About Prompts
9
9
10
- ## SHORT DESCRIPTION
10
+ ## Short description
11
+ Describes the ` Prompt ` function and demonstrates how to create a custom
12
+ ` Prompt ` function.
11
13
12
- Describes the Prompt function and demonstrates how to create a custom Prompt
13
- function.
14
-
15
- ## LONG DESCRIPTION
14
+ ## Long description
16
15
17
16
The PowerShell command prompt indicates that PowerShell is ready to run a
18
17
command:
19
18
20
19
```
21
- PS>
20
+ PS C:\ >
22
21
```
23
22
24
- The PowerShell prompt is determined by the built-in Prompt function. You can
25
- customize the prompt by creating your own Prompt function and saving it in
23
+ The PowerShell prompt is determined by the built-in ` Prompt ` function. You can
24
+ customize the prompt by creating your own ` Prompt ` function and saving it in
26
25
your PowerShell profile.
27
26
28
- ## ABOUT THE PROMPT FUNCTION
27
+ ## About the Prompt function
29
28
30
- The Prompt function determines the appearance of the PowerShell prompt.
31
- PowerShell comes with a built-in Prompt function, but you can override it by
32
- defining your own Prompt function.
29
+ The ` Prompt ` function determines the appearance of the PowerShell prompt.
30
+ PowerShell comes with a built-in ` Prompt ` function, but you can override it by
31
+ defining your own ` Prompt ` function.
33
32
34
- The Prompt function has the following syntax:
33
+ The ` Prompt ` function has the following syntax:
35
34
36
35
``` powershell
37
36
function Prompt { <function-body> }
38
37
```
39
38
40
- The Prompt function must return an object. As a best practice, return a string
41
- or an object that is formatted as a string. The maximum recommended length is
42
- 80 characters.
39
+ The ` Prompt ` function must return an object. As a best practice, return a
40
+ string or an object that is formatted as a string. The maximum recommended
41
+ length is 80 characters.
43
42
44
- For example, the following prompt function returns a "Hello, World" string
45
- followed by a caret (> ).
43
+ For example, the following ` Prompt ` function returns a "Hello, World" string
44
+ followed by a right angle bracket ( ` > ` ).
46
45
47
46
``` powershell
48
- PS> function prompt {"Hello, World > "}
47
+ PS C:\ > function prompt {"Hello, World > "}
49
48
Hello, World >
50
49
```
51
50
52
- ### GETTING THE PROMPT FUNCTION
51
+ ### Getting the Prompt function
53
52
54
- To get the Prompt function, use the ` Get-Command ` cmdlet or use the ` Get-Item `
55
- cmdlet in the Function drive.
53
+ To get the ` Prompt ` function, use the ` Get-Command ` cmdlet or use the
54
+ ` Get-Item ` cmdlet in the Function drive.
56
55
57
56
For example:
58
57
59
58
``` powershell
60
- PS> Get-Command Prompt
59
+ PS C:\ > Get-Command Prompt
61
60
62
61
CommandType Name ModuleName
63
62
----------- ---- ----------
64
63
Function prompt
65
64
```
66
65
67
66
To get the script that sets the value of the prompt, use the dot method to get
68
- the ScriptBlock property of the Prompt function.
67
+ the ** ScriptBlock** property of the ` Prompt ` function.
69
68
70
69
For example:
71
70
@@ -80,68 +79,67 @@ For example:
80
79
# .ExternalHelp System.Management.Automation.dll-help.xml
81
80
```
82
81
83
- Like all functions, the Prompt function is stored in the Function: drive. To
84
- display the script that creates the current Prompt function, type:
82
+ Like all functions, the ` Prompt ` function is stored in the ` Function: ` drive.
83
+ To display the script that creates the current ` Prompt ` function, type:
85
84
86
85
``` powershell
87
86
(Get-Item function:prompt).ScriptBlock
88
87
```
89
88
89
+ ### The default prompt
90
90
91
- ### THE DEFAULT PROMPT
92
-
93
- The default prompt appears only when the Prompt function generates an error or
94
- does not return an object.
91
+ The default prompt appears only when the ` Prompt ` function generates an error
92
+ or does not return an object.
95
93
96
94
The default PowerShell prompt is:
97
95
98
96
```
99
97
PS>
100
98
```
101
99
102
- For example, the following command sets the Prompt function to ` $null ` , which is
103
- invalid. As a result, the default prompt appears.
100
+ For example, the following command sets the ` Prompt ` function to ` $null ` , which
101
+ is invalid. As a result, the default prompt appears.
104
102
105
103
``` powershell
106
- PS> function prompt {$null}
104
+ PS C:\ > function prompt {$null}
107
105
PS>
108
106
```
109
107
110
108
Because PowerShell comes with a built-in prompt, you usually do not see the
111
109
default prompt.
112
110
113
- ### BUILT-IN PROMPT
111
+ ### Built-in prompt
114
112
115
- PowerShell includes a built-in prompt function.
113
+ PowerShell includes a built-in ` Prompt ` function.
116
114
117
115
``` powershell
118
116
function prompt {
119
117
$(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' }
120
- else { '' }) + 'PS ' + $(Get-Location) `
121
- + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
118
+ else { '' }) + 'PS ' + $(Get-Location) +
119
+ $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
122
120
}
123
121
```
124
122
125
- The function uses the Test-Path cmdlet to determine whether the
123
+ The function uses the ` Test-Path ` cmdlet to determine whether the
126
124
` $PSDebugContext ` automatic variable is populated. If ` $PSDebugContext ` is
127
- populated, you are in debugging mode, and " [ DBG] " is added to the prompt, as
125
+ populated, you are in debugging mode, and ` [DBG]: ` is added to the prompt, as
128
126
follows:
129
127
130
128
``` Output
131
- [DBG] PS C:\ps-test>
129
+ [DBG]: PS C:\ps-test>
132
130
```
133
131
134
- If ` $PSDebugContext ` is not populated, the function adds "PS" to the prompt.
135
- And, the function uses the ` Get-Location ` cmdlet to get the current file
136
- system directory location. Then, it adds a right angle bracket (> ).
132
+ If ` $PSDebugContext ` is not populated, the function adds ` PS ` to the prompt.
133
+ And, the function uses the ` Get-Location ` cmdlet to get the current file system
134
+ directory location. Then, it adds a right angle bracket (` > ` ).
137
135
138
136
For example:
139
137
140
138
``` Output
141
139
PS C:\ps-test>
142
140
```
143
141
144
- If you are in a nested prompt, the function adds two angle brackets (>> ) to
142
+ If you are in a nested prompt, the function adds two angle brackets (` >> ` ) to
145
143
the prompt. (You are in a nested prompt if the value of the
146
144
` $NestedPromptLevel ` automatic variable is greater than 1.)
147
145
@@ -152,10 +150,10 @@ the following prompt:
152
150
[DBG] PS C:\ps-test>>>
153
151
```
154
152
155
- ### CHANGES TO THE PROMPT
153
+ ### Changes to the prompt
156
154
157
- The Enter-PSSession cmdlet prepends the name of the remote computer to the
158
- current Prompt function. When you use the Enter-PSSession cmdlet to start a
155
+ The ` Enter-PSSession ` cmdlet prepends the name of the remote computer to the
156
+ current ` Prompt ` function. When you use the ` Enter-PSSession ` cmdlet to start a
159
157
session with a remote computer, the command prompt changes to include the name
160
158
of the remote computer. For example:
161
159
@@ -170,12 +168,12 @@ custom command prompts.
170
168
For more information about the ` $PSDebugContext ` and ` $NestedPromptLevel `
171
169
automatic variables, see [ about_Automatic_Variables] ( about_Automatic_Variables.md ) .
172
170
173
- ### HOW TO CUSTOMIZE THE PROMPT
171
+ ### How to customize the prompt
174
172
175
- To customize the prompt, write a new Prompt function. The function is not
173
+ To customize the prompt, write a new ` Prompt ` function. The function is not
176
174
protected, so you can overwrite it.
177
175
178
- To write a prompt function, type the following:
176
+ To write a ` Prompt ` function, type the following:
179
177
180
178
``` powershell
181
179
function prompt { }
@@ -196,7 +194,7 @@ On the Server01 computer, the prompt resembles the following prompt:
196
194
PS [Server01] >
197
195
```
198
196
199
- The following prompt function includes the current date and time:
197
+ The following ` Prompt ` function includes the current date and time:
200
198
201
199
``` powershell
202
200
function prompt {"$(Get-Date)> "}
@@ -208,11 +206,11 @@ The prompt resembles the following prompt:
208
206
03/15/2012 17:49:47>
209
207
```
210
208
211
- You can also change the default Prompt function:
209
+ You can also change the default ` Prompt ` function:
212
210
213
- For example, the following modified Prompt function adds " [ ADMIN] :" to the
214
- built-in PowerShell prompt when PowerShell is opened by using the "Run as
215
- administrator" option:
211
+ For example, the following modified ` Prompt ` function adds ` [ADMIN]: ` to the
212
+ built-in PowerShell prompt when PowerShell is opened by using the
213
+ ** Run as administrator** option:
216
214
217
215
``` powershell
218
216
function prompt {
@@ -224,18 +222,18 @@ function prompt {
224
222
"Administrator")) { "[ADMIN]: " }
225
223
else { '' }
226
224
) + 'PS ' + $(Get-Location) +
227
- $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
225
+ $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
228
226
}
229
227
```
230
228
231
- When you start PowerShell by using the " Run as administrator" option, a prompt
232
- that resembles the following prompt appears:
229
+ When you start PowerShell by using the ** Run as administrator** option, a
230
+ prompt that resembles the following prompt appears:
233
231
234
232
``` Output
235
233
[ADMIN]: PS C:\ps-test>
236
234
```
237
235
238
- The following Prompt function displays the history ID of the next command. To
236
+ The following ` Prompt ` function displays the history ID of the next command. To
239
237
view the command history, use the ` Get-History ` cmdlet.
240
238
241
239
``` powershell
@@ -254,27 +252,27 @@ function prompt {
254
252
}
255
253
```
256
254
257
- The following prompt uses the Write-Host and Get-Random cmdlets to create a
255
+ The following prompt uses the ` Write-Host ` and ` Get-Random ` cmdlets to create a
258
256
prompt that changes color randomly. Because ` Write-Host ` writes to the current
259
257
host application but does not return an object, this function includes a
260
- Return statement. Without it, PowerShell uses the default prompt, " PS>" .
258
+ ` Return ` statement. Without it, PowerShell uses the default prompt, ` PS> ` .
261
259
262
260
``` powershell
263
261
function prompt {
264
- $color = Get-Random -Minimum 1 -Maximum 16
265
- Write-Host ("PS " + $(Get-Location) +">") -NoNewline `
262
+ $color = Get-Random -Min 1 -Max 16
263
+ Write-Host ("PS " + $(Get-Location) +">") -NoNewLine `
266
264
-ForegroundColor $Color
267
265
return " "
268
266
}
269
267
```
270
268
271
- ### SAVING THE PROMPT FUNCTION
269
+ ### Saving the Prompt function
272
270
273
- Like any function, the Prompt function exists only in the current session. To
274
- save the Prompt function for future sessions, add it to your PowerShell
275
- profiles. For more information about profiles, see about_Profiles.
271
+ Like any function, the ` Prompt ` function exists only in the current session. To
272
+ save the ` Prompt ` function for future sessions, add it to your PowerShell
273
+ profiles. For more information about profiles, see [ about_Profiles] ( about_Profiles.md ) .
276
274
277
- ## SEE ALSO
275
+ ## See also
278
276
279
277
[ Get-Location] ( ../../Microsoft.PowerShell.Management/Get-Location.md )
280
278
@@ -294,4 +292,4 @@ profiles. For more information about profiles, see about_Profiles.
294
292
295
293
[ about_Debuggers] ( about_Debuggers.md )
296
294
297
- [ about_Automatic_Variables] ( about_Automatic_Variables.md )
295
+ [ about_Automatic_Variables] ( about_Automatic_Variables.md )
0 commit comments