Skip to content

Commit 5670970

Browse files
committed
Document Command Path Precedence
Addresses #4105
1 parent d29e50a commit 5670970

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
ms.date: 05/17/2019
3+
schema: 2.0.0
4+
locale: en-us
5+
keywords: powershell,cmdlet
6+
title: about_Execution_Search
7+
---
8+
9+
# Command Path Precedence
10+
11+
## Short description
12+
13+
When you run command by path,
14+
such as `./myScript.ps1` PowerShell has a process that it uses to search for the script.
15+
This document describes how PowerShell does that search.
16+
17+
## Long description
18+
19+
### Limiting the search to the current directory
20+
21+
To tell PowerShell that you want to search for the script in the current folder,
22+
you should specify the prefix `.\`.
23+
This will limit the search for commands to files in the current folder.
24+
Without this prefix,
25+
other PowerShell syntax may conflict and there are few guarantees that the file will be found.
26+
27+
### Using Wildcards in Execution
28+
29+
> [!NOTE]
30+
> Using wildcard characters is sometimes referred to as *globbing*.
31+
32+
You may use wildcards in command execution.
33+
PowerShell supports the following wildcards.
34+
35+
| Wildcard character | Description | Example | Matches | Does not match |
36+
|--------------------|---------------------------------------------------------------------|----------|------------------|----------------|
37+
| * | Matches zero or more characters, starting at the specified position | a* | A, ag, Apple | |
38+
| ? | Matches any character at the specified position | ?n | An, in, on | ran |
39+
| [ ] | Matches a range of characters | [a-l]ook | book, cook, look | took |
40+
| [ ] | Matches the specified characters | [bc]ook | book, cook | look |
41+
42+
### Path Precedence
43+
44+
PowerShell will execute a file that has a wildcard match, before a literal match.
45+
46+
For example, consider a directory with the following files:
47+
48+
```
49+
a.ps1
50+
[a1].ps1
51+
```
52+
53+
Then, you are in PowerShell and have `Set-Location` to this folder.
54+
You run `[a1].ps1`, the file `a.ps1`,
55+
even though the file `[a1].ps1` matches this based on the literal match.
56+
57+
However, if the directory looked like this:
58+
59+
```
60+
b.ps1
61+
[a1].ps1
62+
```
63+
64+
and you execute `[b1].ps1` because there is no literal match,
65+
but `b.ps1` matches based on the wildcard, `b.ps1` will be executed.
66+
67+
## See also
68+
69+
- [about_Command_Precedence](about_Command_Precedence.md)

reference/5.1/Microsoft.PowerShell.Core/About/about_Command_Precedence.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ following rules.
4848
.\FindDocs.ps1
4949
```
5050

51+
For additional details, see [Command Path Precedence](about_Command_Path_Precedence.md)
52+
5153
- If you do not specify a path, PowerShell uses the following precedence order
5254
when it runs commands:
5355

0 commit comments

Comments
 (0)