Skip to content

Commit a9a1bcf

Browse files
authored
Closes #1582 - style cleanup (#4887)
* Closes #1582 - style cleanup * feedback edits
1 parent d0d03f0 commit a9a1bcf

File tree

6 files changed

+401
-405
lines changed

6 files changed

+401
-405
lines changed
Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,70 @@
11
---
2-
ms.date: 11/30/2017
2+
ms.date: 10/04/2019
33
schema: 2.0.0
44
locale: en-us
55
keywords: powershell,cmdlet
66
title: about_Prompts
77
---
88
# About Prompts
99

10-
## SHORT DESCRIPTION
10+
## Short description
11+
Describes the `Prompt` function and demonstrates how to create a custom
12+
`Prompt` function.
1113

12-
Describes the Prompt function and demonstrates how to create a custom Prompt
13-
function.
14-
15-
## LONG DESCRIPTION
14+
## Long description
1615

1716
The PowerShell command prompt indicates that PowerShell is ready to run a
1817
command:
1918

2019
```
21-
PS>
20+
PS C:\>
2221
```
2322

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
2625
your PowerShell profile.
2726

28-
## ABOUT THE PROMPT FUNCTION
27+
## About the Prompt function
2928

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.
3332

34-
The Prompt function has the following syntax:
33+
The `Prompt` function has the following syntax:
3534

3635
```powershell
3736
function Prompt { <function-body> }
3837
```
3938

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.
4342

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 (`>`).
4645

4746
```powershell
48-
PS> function prompt {"Hello, World > "}
47+
PS C:\> function prompt {"Hello, World > "}
4948
Hello, World >
5049
```
5150

52-
### GETTING THE PROMPT FUNCTION
51+
### Getting the Prompt function
5352

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.
5655

5756
For example:
5857

5958
```powershell
60-
PS> Get-Command Prompt
59+
PS C:\> Get-Command Prompt
6160
6261
CommandType Name ModuleName
6362
----------- ---- ----------
6463
Function prompt
6564
```
6665

6766
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.
6968

7069
For example:
7170

@@ -80,68 +79,67 @@ For example:
8079
# .ExternalHelp System.Management.Automation.dll-help.xml
8180
```
8281

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:
8584

8685
```powershell
8786
(Get-Item function:prompt).ScriptBlock
8887
```
8988

89+
### The default prompt
9090

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.
9593

9694
The default PowerShell prompt is:
9795

9896
```
9997
PS>
10098
```
10199

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.
104102

105103
```powershell
106-
PS> function prompt {$null}
104+
PS C:\> function prompt {$null}
107105
PS>
108106
```
109107

110108
Because PowerShell comes with a built-in prompt, you usually do not see the
111109
default prompt.
112110

113-
### BUILT-IN PROMPT
111+
### Built-in prompt
114112

115-
PowerShell includes a built-in prompt function.
113+
PowerShell includes a built-in `Prompt` function.
116114

117115
```powershell
118116
function prompt {
119117
$(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) { '>>' }) + '> '
122120
}
123121
```
124122

125-
The function uses the Test-Path cmdlet to determine whether the
123+
The function uses the `Test-Path` cmdlet to determine whether the
126124
`$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
128126
follows:
129127

130128
```Output
131-
[DBG] PS C:\ps-test>
129+
[DBG]: PS C:\ps-test>
132130
```
133131

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 (`>`).
137135

138136
For example:
139137

140138
```Output
141139
PS C:\ps-test>
142140
```
143141

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
145143
the prompt. (You are in a nested prompt if the value of the
146144
`$NestedPromptLevel` automatic variable is greater than 1.)
147145

@@ -152,10 +150,10 @@ the following prompt:
152150
[DBG] PS C:\ps-test>>>
153151
```
154152

155-
### CHANGES TO THE PROMPT
153+
### Changes to the prompt
156154

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
159157
session with a remote computer, the command prompt changes to include the name
160158
of the remote computer. For example:
161159

@@ -170,12 +168,12 @@ custom command prompts.
170168
For more information about the `$PSDebugContext` and `$NestedPromptLevel`
171169
automatic variables, see [about_Automatic_Variables](about_Automatic_Variables.md).
172170

173-
### HOW TO CUSTOMIZE THE PROMPT
171+
### How to customize the prompt
174172

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
176174
protected, so you can overwrite it.
177175

178-
To write a prompt function, type the following:
176+
To write a `Prompt` function, type the following:
179177

180178
```powershell
181179
function prompt { }
@@ -196,7 +194,7 @@ On the Server01 computer, the prompt resembles the following prompt:
196194
PS [Server01] >
197195
```
198196

199-
The following prompt function includes the current date and time:
197+
The following `Prompt` function includes the current date and time:
200198

201199
```powershell
202200
function prompt {"$(Get-Date)> "}
@@ -208,11 +206,11 @@ The prompt resembles the following prompt:
208206
03/15/2012 17:49:47>
209207
```
210208

211-
You can also change the default Prompt function:
209+
You can also change the default `Prompt` function:
212210

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:
216214

217215
```powershell
218216
function prompt {
@@ -224,18 +222,18 @@ function prompt {
224222
"Administrator")) { "[ADMIN]: " }
225223
else { '' }
226224
) + 'PS ' + $(Get-Location) +
227-
$(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
225+
$(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
228226
}
229227
```
230228

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:
233231

234232
```Output
235233
[ADMIN]: PS C:\ps-test>
236234
```
237235

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
239237
view the command history, use the `Get-History` cmdlet.
240238

241239
```powershell
@@ -254,27 +252,27 @@ function prompt {
254252
}
255253
```
256254

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
258256
prompt that changes color randomly. Because `Write-Host` writes to the current
259257
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>`.
261259

262260
```powershell
263261
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 `
266264
-ForegroundColor $Color
267265
return " "
268266
}
269267
```
270268

271-
### SAVING THE PROMPT FUNCTION
269+
### Saving the Prompt function
272270

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).
276274

277-
## SEE ALSO
275+
## See also
278276

279277
[Get-Location](../../Microsoft.PowerShell.Management/Get-Location.md)
280278

@@ -294,4 +292,4 @@ profiles. For more information about profiles, see about_Profiles.
294292

295293
[about_Debuggers](about_Debuggers.md)
296294

297-
[about_Automatic_Variables](about_Automatic_Variables.md)
295+
[about_Automatic_Variables](about_Automatic_Variables.md)

0 commit comments

Comments
 (0)