Skip to content

Commit 3a1d191

Browse files
committed
Edit about_Command_Precedence.md
1 parent e900672 commit 3a1d191

File tree

5 files changed

+470
-360
lines changed

5 files changed

+470
-360
lines changed
Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
2-
ms.date: 2017-06-09
2+
ms.date: 2017-06-27
33
schema: 2.0.0
4-
locale: en-us
54
keywords: powershell,cmdlet
65
title: about_Command_Precedence
76
---
@@ -31,66 +30,64 @@ PowerShell uses the following rules to decide which command to run.
3130
These rules become very important when you add commands to your session
3231
from modules, snap-ins, and other sessions.
3332

34-
-- If you specify the path to a command, Windows PowerShell runs the
35-
command at the location specified by the path.
33+
- If you specify the path to a command, Windows PowerShell runs the command at the location specified by the path.
3634

3735
For example, the following command runs the FindDocs.ps1
38-
script in the C:\TechDocs directory:
36+
script in the "C:\TechDocs" directory:
3937

38+
```
4039
C:\TechDocs\FindDocs.ps1
40+
```
4141

4242
As a security feature, Windows PowerShell does not run executable
4343
(native) commands, including Windows PowerShell scripts, unless the
4444
command is located in a path that is listed in the Path environment
45-
variable ($env:path) or unless you specify the path to the script
45+
variable `($env:path)` or unless you specify the path to the script
4646
file.
4747

4848
To run a script that is in the current directory, specify the full
49-
path, or type a dot (.) to represent the current directory.
49+
path, or type a dot `(.)` to represent the current directory.
5050

5151
For example, to run the FindDocs.ps1 file in the current directory,
5252
type:
5353

54+
```
5455
.\FindDocs.ps1
56+
```
5557

56-
-- If you do not specify a path, Windows PowerShell uses the following
57-
precedence order when it runs commands:
58+
- If you do not specify a path, Windows PowerShell uses the following precedence order when it runs commands:
5859

59-
1. Alias
60-
2. Function
61-
3. Cmdlet
62-
4. Native Windows commands
60+
1. Alias
61+
2. Function
62+
3. Cmdlet
63+
4. Native Windows commands
6364

6465
Therefore, if you type "help", Windows PowerShell first looks for an
6566
alias named "help", then a function named "Help", and finally a
6667
cmdlet named "Help". It runs the first "help" item that it finds.
6768

68-
For example, if you have a Get-Map function in the session and you
69-
import a cmdlet named Get-Map. By default, when you type "Get-Map",
70-
Windows PowerShell runs the Get-Map function.
69+
For example, if you have a "Get-Map" function in the session and you
70+
import a cmdlet named "Get-Map". By default, when you type "Get-Map",
71+
Windows PowerShell runs the "Get-Map" function.
7172

72-
-- When the session contains items of the same type that have the same
73-
name, such as two cmdlets with the same name, Windows PowerShell
74-
runs the item that was added to the session most recently.
73+
- When the session contains items of the same type that have the same name, such as two cmdlets with the same name, Windows PowerShell runs the item that was added to the session most recently.
7574

76-
For example, if you have a cmdlet named Get-Date and you import
77-
another cmdlet named Get-Date, by default, Windows PowerShell runs
75+
For example, if you have a cmdlet named "Get-Date" and you import
76+
another cmdlet named "Get-Date", by default, Windows PowerShell runs
7877
the most-recently imported cmdlet when you type "Get-Date".
7978

80-
HIDDEN and REPLACED ITEMS
79+
# HIDDEN and REPLACED ITEMS
80+
8181
As a result of these rules, items can be replaced or hidden by items with
8282
the same name.
8383

84-
-- Items are "hidden" or "shadowed" if you can still access the original
85-
item, such as by qualifying the item name with a module or snap-in
86-
name.
84+
- Items are "hidden" or "shadowed" if you can still access the original item, such as by qualifying the item name with a module or snap-in name.
8785

8886
For example, if you import a function that has the same name as a
8987
cmdlet in the session, the cmdlet is hidden (but not replaced)
9088
because it was imported from a snap-in or module.
9189

92-
-- Items are "replaced" or "overwritten" if you can no longer access
93-
the original item.
90+
- Items are "replaced" or "overwritten" if you can no longer access the original item.
9491

9592
For example, if you import a variable that has the same name as a
9693
a variable in the session, the original variable is replaced and is
@@ -103,30 +100,34 @@ is no longer accessible.
103100

104101
# FINDING HIDDEN COMMANDS
105102

106-
The All parameter of the Get-Command cmdlet gets all commands with the
103+
The **All** parameter of the [Get-Command](../Get-Command.md) cmdlet gets all commands with the
107104
specified name, even if they are hidden or replaced. Beginning in Windows
108-
PowerShell 3.0, by default, Get-Command gets only the commands that run
105+
PowerShell 3.0, by default, `Get-Command` gets only the commands that run
109106
when you type the command name.
110107

111-
In the following examples, the session includes a Get-Date function and a
112-
Get-Date cmdlet.
108+
In the following examples, the session includes a "Get-Date" function and a
109+
[Get-Date](../../Microsoft.PowerShell.Utility/Get-Date.md) cmdlet.
113110

114-
The following command gets the Get-Date command that runs when you type "Get-Date".
111+
The following command gets the "Get-Date" command that runs when you type "Get-Date".
115112

116-
PS C:> Get-Command Get-Date
113+
```powershell
114+
Get-Command Get-Date
117115
118116
CommandType Name ModuleName
119117
----------- ---- ----------
120-
Function get-date
118+
Function Get-Date
119+
```
121120

122-
The following command uses the All parameter to get all Get-Date commands.
121+
The following command uses the **All** parameter to get all "Get-Date" commands.
123122

124-
PS C:> Get-Command Get-Date -All
123+
```powershell
124+
Get-Command Get-Date -All
125125
126126
CommandType Name ModuleName
127127
----------- ---- ----------
128-
Function get-date
128+
Function Get-Date
129129
Cmdlet Get-Date Microsoft.PowerShell.Utility
130+
```
130131

131132
# RUNNING HIDDEN COMMANDS
132133

@@ -149,72 +150,92 @@ name with the name of the module or snap-in in which it originated.
149150

150151
You can qualify commands, but you cannot qualify variables or aliases.
151152

152-
For example, if the Get-Date cmdlet from the Microsoft.PowerShell.Utility
153+
For example, if the `Get-Date` cmdlet from the `Microsoft.PowerShell.Utility`
153154
snap-in is hidden by an alias, function, or cmdlet with the same name, you
154155
can run it by using the snap-in-qualified name of the cmdlet:
155156

157+
```powershell
156158
Microsoft.PowerShell.Utility\Get-Date
159+
```
157160

158-
To run a New-Map command that was added by the MapFunctions module, use
161+
To run a "New-Map" command that was added by the "MapFunctions" module, use
159162
its module-qualified name:
160163

164+
```powershell
161165
MapFunctions\New-Map
166+
```
162167

163168
To find the snap-in or module from which a command was imported, use the
164169
ModuleName property of commands.
165170

171+
```powershell
166172
(Get-Command <command-name>).ModuleName
173+
```
167174

168-
For example, to find the source of the Get-Date cmdlet, type:
175+
For example, to find the source of the `Get-Date` cmdlet, type:
169176

170-
PS C:> (Get-Command Get-Date).ModuleName
177+
```powershell
178+
(Get-Command Get-Date).ModuleName
171179
Microsoft.PowerShell.Utility
180+
```
172181

173182
# CALL OPERATOR
174183

175-
You can also use the Call operator (&) to run any command that you
176-
can get by using a Get-ChildItem (the alias is "dir"), Get-Command, or
177-
Get-Module command.
184+
You can also use the `Call` operator `(&)` to run any command that you
185+
can get by using a [Get-ChildItem](../../Microsoft.PowerShell.Management/Get-ChildItem.md) (the alias is "dir"), `Get-Command` or
186+
[Get-Module](../Get-Module.md) command.
178187

179-
To run a command, enclose the Get-Command command in parentheses,
180-
and use the Call operator (&) to run the command.
188+
To run a command, enclose the `Get-Command` command in parentheses,
189+
and use the Call operator `(&)` to run the command.
181190

191+
```powershell
182192
&(Get-Command ...)
193+
```
183194

184-
- or -
195+
or
185196

197+
```powershell
186198
&(dir ... )
199+
```
187200

188-
For example, if you have a function named Map that is hidden by an
189-
alias named Map, use the following command to run the function.
201+
For example, if you have a function named "Map" that is hidden by an
202+
alias named "Map", use the following command to run the function.
190203

191-
&(Get-Command -Name Map -Type function)
204+
```powershell
205+
&(Get-Command -Name Map -Type Function)
206+
```
192207

193-
- or -
208+
or
194209

195-
&(dir function:\map)
210+
```powershell
211+
&(dir Function:\map)
212+
```
196213

197214
You can also save your hidden command in a variable to make it
198215
easier to run.
199216

200-
For example, the following command saves the Map function in the
201-
$myMap variable and then uses the Call operator to run it.
217+
For example, the following command saves the "Map" function in the
218+
"$myMap" variable and then uses the `Call` operator to run it.
202219

220+
```powershell
203221
$myMap = (Get-Command -Name map -Type function)
204-
205222
&($myMap)
223+
```
206224

207225
If a command originated in a module, you can use the following
208226
format to run it.
209227

228+
```
210229
& <PSModuleInfo-object> <command>
230+
```
211231

212-
For example, to run the Add-File cmdlet in the FileCommands
232+
For example, to run the "Add-File" cmdlet in the "FileCommands"
213233
module, use the following command sequence.
214234

215-
$FileCommands = get-module -name FileCommands
216-
235+
```powershell
236+
$FileCommands = Get-Module -Name FileCommands
217237
& $FileCommands Add-File
238+
```
218239

219240
# REPLACED ITEMS
220241

@@ -227,8 +248,8 @@ Variables and aliases are always replaced even if they have been
227248
imported from a module or snap-in because you cannot use a call operator
228249
or a qualified name to run them.
229250

230-
For example, if you type a Get-Map function in your session, and you
231-
import a function called Get-Map, the original function is replaced.
251+
For example, if you type a "Get-Map" function in your session, and you
252+
import a function called "Get-Map", the original function is replaced.
232253
You cannot retrieve it in the current session.
233254

234255
# AVOIDING NAME CONFLICTS
@@ -239,17 +260,19 @@ be unique. For example, add your initials or company name acronym to the
239260
nouns in your commands.
240261

241262
Also, when you import commands into your session from a Windows PowerShell
242-
module or from another session, use the Prefix parameter of the
243-
Import-Module or Import-PSSession cmdlet to add a prefix to the nouns
263+
module or from another session, use the **Prefix** parameter of the
264+
[Import-Module](../Import-Module.md) or [Import-PSSession](../../Microsoft.PowerShell.Utility/Import-PSSession.md) cmdlet to add a prefix to the nouns
244265
in the names of commands.
245266

246-
For example, the following command avoids any conflict with the Get-Date
247-
and Set-Date cmdlets that come with Windows PowerShell when you import
248-
the DateFunctions module.
267+
For example, the following command avoids any conflict with the `Get-Date`
268+
and [Set-Date](../../Microsoft.PowerShell.Utility/Set-Date.md) cmdlets that come with Windows PowerShell when you import
269+
the `DateFunctions` module.
249270

271+
```powershell
250272
Import-Module -Name DateFunctions -Prefix ZZ
273+
```
251274

252-
For more information, see Import-Module and Import-PSSession.
275+
For more information, see `Import-Module` and `Import-PSSession` below.
253276

254277
# SEE ALSO
255278

@@ -259,13 +282,12 @@ For more information, see Import-Module and Import-PSSession.
259282

260283
[about_Functions](about_Functions.md)
261284

262-
Alias (provider)
263-
264-
Function (provider)
285+
[Alias-Provider](../Providers/Alias-Provider.md)
265286

266-
Get-Command
287+
[Function-Provider](../Providers/Function-Provider.md)
267288

268-
Import-Module
289+
[Get-Command](../Get-Command.md)
269290

270-
Import-PSSession
291+
[Import-Module](../Import-Module.md)
271292

293+
[Import-PSSession](../../Microsoft.PowerShell.Utility/Import-PSSession.md)

0 commit comments

Comments
 (0)