Skip to content

Commit bb6a957

Browse files
authored
Add note about double escaping (MicrosoftDocs#12479)
1 parent e7dd518 commit bb6a957

File tree

4 files changed

+288
-24
lines changed

4 files changed

+288
-24
lines changed

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

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to use wildcard characters in PowerShell.
33
Locale: en-US
4-
ms.date: 05/14/2024
4+
ms.date: 11/03/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_wildcards?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Wildcards
@@ -61,11 +61,6 @@ through **l**, type:
6161
Get-ChildItem C:\Techdocs\[a-l]*.txt
6262
```
6363

64-
> [!NOTE]
65-
> Wildcard matching for filesystem items works differently than for strings.
66-
> For more information, see the _Remarks_ section of the
67-
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
68-
6964
There may be cases where you want to match the literal character rather than
7065
treat it as a wildcard character. In those cases you can use the backtick
7166
(`` ` ``) character to escape the wildcard character so that it is compared
@@ -99,6 +94,77 @@ foreach ($point in $p) {
9994
}
10095
```
10196

97+
## Escaping wildcard characters in file and directory names
98+
99+
> [!NOTE]
100+
> Wildcard matching for filesystem items works differently than for strings.
101+
> For more information, see the _Remarks_ section of the
102+
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
103+
104+
When you try to access a file or directory that contains wildcard characters
105+
the name, you must escape the wildcard characters. Consider the following files:
106+
107+
```powershell
108+
PS> Get-ChildItem
109+
110+
Directory: D:\temp\test
111+
112+
Mode LastWriteTime Length Name
113+
---- ------------- ------ ----
114+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
115+
-a--- 11/3/2025 3:39 PM 41 file[2].txt
116+
-a--- 11/3/2025 3:39 PM 41 file[3].txt
117+
```
118+
119+
The square-bracket (`[]`) characters are wildcards so they must be escaped when
120+
trying to get the file using one of the Item cmdlets, such as `Get-Item`.
121+
122+
```powershell
123+
PS> Get-Item file`[1`].txt
124+
```
125+
126+
However, this example failed because the filename value is bound to the
127+
**Path** parameter, which supports wildcard characters. In this case, the
128+
`` `[ `` pattern resolves to plain `[`, which the **Path** parameter interprets
129+
as a wildcard. There are three ways to resolve this:
130+
131+
- Escape the backtick characters.
132+
133+
```powershell
134+
PS> Get-Item -Path file``[1``].txt
135+
136+
Directory: D:\temp\test
137+
138+
Mode LastWriteTime Length Name
139+
---- ------------- ------ ----
140+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
141+
```
142+
143+
- Put the filename in single quotes so that the backticks aren't expanded
144+
before being bound to the **Path** parameter.
145+
146+
```powershell
147+
PS> Get-Item -Path 'file`[1`].txt'
148+
149+
Directory: D:\temp\test
150+
151+
Mode LastWriteTime Length Name
152+
---- ------------- ------ ----
153+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
154+
```
155+
156+
- Use the **LiteralPath** parameter
157+
158+
```powershell
159+
PS> Get-Item -LiteralPath file[1].txt
160+
161+
Directory: D:\temp\test
162+
163+
Mode LastWriteTime Length Name
164+
---- ------------- ------ ----
165+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
166+
```
167+
102168
## See also
103169

104170
- [about_If][02]

reference/7.4/Microsoft.PowerShell.Core/About/about_Wildcards.md

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to use wildcard characters in PowerShell.
33
Locale: en-US
4-
ms.date: 05/14/2024
4+
ms.date: 11/03/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_wildcards?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Wildcards
@@ -61,11 +61,6 @@ through **l**, type:
6161
Get-ChildItem C:\Techdocs\[a-l]*.txt
6262
```
6363

64-
> [!NOTE]
65-
> Wildcard matching for filesystem items works differently than for strings.
66-
> For more information, see the _Remarks_ section of the
67-
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
68-
6964
There may be cases where you want to match the literal character rather than
7065
treat it as a wildcard character. In those cases you can use the backtick
7166
(`` ` ``) character to escape the wildcard character so that it is compared
@@ -99,6 +94,77 @@ foreach ($point in $p) {
9994
}
10095
```
10196

97+
## Escaping wildcard characters in file and directory names
98+
99+
> [!NOTE]
100+
> Wildcard matching for filesystem items works differently than for strings.
101+
> For more information, see the _Remarks_ section of the
102+
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
103+
104+
When you try to access a file or directory that contains wildcard characters
105+
the name, you must escape the wildcard characters. Consider the following files:
106+
107+
```powershell
108+
PS> Get-ChildItem
109+
110+
Directory: D:\temp\test
111+
112+
Mode LastWriteTime Length Name
113+
---- ------------- ------ ----
114+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
115+
-a--- 11/3/2025 3:39 PM 41 file[2].txt
116+
-a--- 11/3/2025 3:39 PM 41 file[3].txt
117+
```
118+
119+
The square-bracket (`[]`) characters are wildcards so they must be escaped when
120+
trying to get the file using one of the Item cmdlets, such as `Get-Item`.
121+
122+
```powershell
123+
PS> Get-Item file`[1`].txt
124+
```
125+
126+
However, this example failed because the filename value is bound to the
127+
**Path** parameter, which supports wildcard characters. In this case, the
128+
`` `[ `` pattern resolves to plain `[`, which the **Path** parameter interprets
129+
as a wildcard. There are three ways to resolve this:
130+
131+
- Escape the backtick characters.
132+
133+
```powershell
134+
PS> Get-Item -Path file``[1``].txt
135+
136+
Directory: D:\temp\test
137+
138+
Mode LastWriteTime Length Name
139+
---- ------------- ------ ----
140+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
141+
```
142+
143+
- Put the filename in single quotes so that the backticks aren't expanded
144+
before being bound to the **Path** parameter.
145+
146+
```powershell
147+
PS> Get-Item -Path 'file`[1`].txt'
148+
149+
Directory: D:\temp\test
150+
151+
Mode LastWriteTime Length Name
152+
---- ------------- ------ ----
153+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
154+
```
155+
156+
- Use the **LiteralPath** parameter
157+
158+
```powershell
159+
PS> Get-Item -LiteralPath file[1].txt
160+
161+
Directory: D:\temp\test
162+
163+
Mode LastWriteTime Length Name
164+
---- ------------- ------ ----
165+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
166+
```
167+
102168
## See also
103169

104170
- [about_If][02]

reference/7.5/Microsoft.PowerShell.Core/About/about_Wildcards.md

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to use wildcard characters in PowerShell.
33
Locale: en-US
4-
ms.date: 05/14/2024
4+
ms.date: 11/03/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_wildcards?view=powershell-7.5&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Wildcards
@@ -61,11 +61,6 @@ through **l**, type:
6161
Get-ChildItem C:\Techdocs\[a-l]*.txt
6262
```
6363

64-
> [!NOTE]
65-
> Wildcard matching for filesystem items works differently than for strings.
66-
> For more information, see the _Remarks_ section of the
67-
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
68-
6964
There may be cases where you want to match the literal character rather than
7065
treat it as a wildcard character. In those cases you can use the backtick
7166
(`` ` ``) character to escape the wildcard character so that it is compared
@@ -99,6 +94,77 @@ foreach ($point in $p) {
9994
}
10095
```
10196

97+
## Escaping wildcard characters in file and directory names
98+
99+
> [!NOTE]
100+
> Wildcard matching for filesystem items works differently than for strings.
101+
> For more information, see the _Remarks_ section of the
102+
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
103+
104+
When you try to access a file or directory that contains wildcard characters
105+
the name, you must escape the wildcard characters. Consider the following files:
106+
107+
```powershell
108+
PS> Get-ChildItem
109+
110+
Directory: D:\temp\test
111+
112+
Mode LastWriteTime Length Name
113+
---- ------------- ------ ----
114+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
115+
-a--- 11/3/2025 3:39 PM 41 file[2].txt
116+
-a--- 11/3/2025 3:39 PM 41 file[3].txt
117+
```
118+
119+
The square-bracket (`[]`) characters are wildcards so they must be escaped when
120+
trying to get the file using one of the Item cmdlets, such as `Get-Item`.
121+
122+
```powershell
123+
PS> Get-Item file`[1`].txt
124+
```
125+
126+
However, this example failed because the filename value is bound to the
127+
**Path** parameter, which supports wildcard characters. In this case, the
128+
`` `[ `` pattern resolves to plain `[`, which the **Path** parameter interprets
129+
as a wildcard. There are three ways to resolve this:
130+
131+
- Escape the backtick characters.
132+
133+
```powershell
134+
PS> Get-Item -Path file``[1``].txt
135+
136+
Directory: D:\temp\test
137+
138+
Mode LastWriteTime Length Name
139+
---- ------------- ------ ----
140+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
141+
```
142+
143+
- Put the filename in single quotes so that the backticks aren't expanded
144+
before being bound to the **Path** parameter.
145+
146+
```powershell
147+
PS> Get-Item -Path 'file`[1`].txt'
148+
149+
Directory: D:\temp\test
150+
151+
Mode LastWriteTime Length Name
152+
---- ------------- ------ ----
153+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
154+
```
155+
156+
- Use the **LiteralPath** parameter
157+
158+
```powershell
159+
PS> Get-Item -LiteralPath file[1].txt
160+
161+
Directory: D:\temp\test
162+
163+
Mode LastWriteTime Length Name
164+
---- ------------- ------ ----
165+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
166+
```
167+
102168
## See also
103169

104170
- [about_If][02]

reference/7.6/Microsoft.PowerShell.Core/About/about_Wildcards.md

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to use wildcard characters in PowerShell.
33
Locale: en-US
4-
ms.date: 05/14/2024
4+
ms.date: 11/03/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_wildcards?view=powershell-7.6&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Wildcards
@@ -61,11 +61,6 @@ through **l**, type:
6161
Get-ChildItem C:\Techdocs\[a-l]*.txt
6262
```
6363

64-
> [!NOTE]
65-
> Wildcard matching for filesystem items works differently than for strings.
66-
> For more information, see the _Remarks_ section of the
67-
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
68-
6964
There may be cases where you want to match the literal character rather than
7065
treat it as a wildcard character. In those cases you can use the backtick
7166
(`` ` ``) character to escape the wildcard character so that it is compared
@@ -99,6 +94,77 @@ foreach ($point in $p) {
9994
}
10095
```
10196

97+
## Escaping wildcard characters in file and directory names
98+
99+
> [!NOTE]
100+
> Wildcard matching for filesystem items works differently than for strings.
101+
> For more information, see the _Remarks_ section of the
102+
> [DirectoryInfo.GetFiles(String, EnumerationOptions)][05] method.
103+
104+
When you try to access a file or directory that contains wildcard characters
105+
the name, you must escape the wildcard characters. Consider the following files:
106+
107+
```powershell
108+
PS> Get-ChildItem
109+
110+
Directory: D:\temp\test
111+
112+
Mode LastWriteTime Length Name
113+
---- ------------- ------ ----
114+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
115+
-a--- 11/3/2025 3:39 PM 41 file[2].txt
116+
-a--- 11/3/2025 3:39 PM 41 file[3].txt
117+
```
118+
119+
The square-bracket (`[]`) characters are wildcards so they must be escaped when
120+
trying to get the file using one of the Item cmdlets, such as `Get-Item`.
121+
122+
```powershell
123+
PS> Get-Item file`[1`].txt
124+
```
125+
126+
However, this example failed because the filename value is bound to the
127+
**Path** parameter, which supports wildcard characters. In this case, the
128+
`` `[ `` pattern resolves to plain `[`, which the **Path** parameter interprets
129+
as a wildcard. There are three ways to resolve this:
130+
131+
- Escape the backtick characters.
132+
133+
```powershell
134+
PS> Get-Item -Path file``[1``].txt
135+
136+
Directory: D:\temp\test
137+
138+
Mode LastWriteTime Length Name
139+
---- ------------- ------ ----
140+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
141+
```
142+
143+
- Put the filename in single quotes so that the backticks aren't expanded
144+
before being bound to the **Path** parameter.
145+
146+
```powershell
147+
PS> Get-Item -Path 'file`[1`].txt'
148+
149+
Directory: D:\temp\test
150+
151+
Mode LastWriteTime Length Name
152+
---- ------------- ------ ----
153+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
154+
```
155+
156+
- Use the **LiteralPath** parameter
157+
158+
```powershell
159+
PS> Get-Item -LiteralPath file[1].txt
160+
161+
Directory: D:\temp\test
162+
163+
Mode LastWriteTime Length Name
164+
---- ------------- ------ ----
165+
-a--- 11/3/2025 3:39 PM 41 file[1].txt
166+
```
167+
102168
## See also
103169

104170
- [about_If][02]

0 commit comments

Comments
 (0)