Skip to content

Commit fdde4c9

Browse files
committed
Add notes about recursion behavior
1 parent 5375296 commit fdde4c9

File tree

4 files changed

+240
-296
lines changed

4 files changed

+240
-296
lines changed

reference/5.1/Microsoft.PowerShell.Management/Get-ChildItem.md

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,6 @@ Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>]
3333
[<CommonParameters>]
3434
```
3535

36-
### Items (Default) - Certificate provider
37-
38-
```
39-
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>]
40-
[-Exclude <string[]>] [-Recurse] [-Depth <uint>] [-Force] [-Name]
41-
[-CodeSigningCert] [-DocumentEncryptionCert] [-SSLServerAuthentication]
42-
[-DnsName <string>] [-Eku <string[]>] [-ExpiringInDays <int>]
43-
[<CommonParameters>]
44-
```
45-
46-
### LiteralItems - Certificate provider
47-
48-
```
49-
Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>]
50-
[-Exclude <string[]>] [-Recurse] [-Depth <uint>] [-Force] [-Name]
51-
[-CodeSigningCert] [-DocumentEncryptionCert] [-SSLServerAuthentication]
52-
[-DnsName <string>] [-Eku <string[]>] [-ExpiringInDays <int>]
53-
[<CommonParameters>]
54-
```
55-
56-
### Items (Default) - Filesystem provider
57-
58-
```
59-
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>]
60-
[-Exclude <string[]>] [-Recurse] [-Depth <uint>] [-Force] [-Name]
61-
[-Attributes <FlagsExpression[FileAttributes]>] [-FollowSymlink] [-Directory]
62-
[-File] [-Hidden] [-ReadOnly] [-System] [<CommonParameters>]
63-
```
64-
65-
### LiteralItems - FileSystem provider
66-
67-
```
68-
Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>]
69-
[-Exclude <string[]>] [-Recurse] [-Depth <uint>] [-Force] [-Name]
70-
[-Attributes <FlagsExpression[FileAttributes]>] [-FollowSymlink] [-Directory]
71-
[-File] [-Hidden] [-ReadOnly] [-System] [<CommonParameters>]
72-
```
73-
7436
## DESCRIPTION
7537

7638
The `Get-ChildItem` cmdlet gets the items in one or more specified locations. If the item is a
@@ -81,9 +43,10 @@ of levels to recurse.
8143
`Get-ChildItem` doesn't display empty directories. When a `Get-ChildItem` command includes the
8244
**Depth** or **Recurse** parameters, empty directories aren't included in the output.
8345

84-
Locations are exposed to `Get-ChildItem` by PowerShell providers. A location can be a file system
85-
directory, registry hive, or a certificate store. Some parameters are only available for a specific
86-
provider. For more information, see
46+
The `Get-ChildItem` cmdlet is designed to work with the items exposed by any provider. For example,
47+
items can be a file system file or directory, registry hive, or a certificate store. To list the
48+
providers available in your session, use the `Get-PSProvider` command. Some parameters are only
49+
available for a specific provider. For more information, see
8750
[about_Providers](../Microsoft.PowerShell.Core/About/about_Providers.md).
8851

8952
## EXAMPLES
@@ -125,7 +88,7 @@ as follows:
12588
- `s` (system)
12689

12790
For more information about the mode flags, see
128-
[about_Filesystem_Provider](../microsoft.powershell.core/about/about_filesystem_provider.md#attributes-flagsexpression).
91+
[about_Filesystem_Provider](../Microsoft.PowerShell.Core/About/about_Filesystem_Provider.md#attributes-flagsexpression).
12992

13093
### Example 2: Get child item names in a directory
13194

@@ -347,8 +310,8 @@ authority.
347310
Get-ChildItem -Path Cert:\* -Recurse -CodeSigningCert
348311
```
349312

350-
For more information about the Certificate provider and the `Cert:` drive,
351-
see [about_Certificate_Provider](../Microsoft.PowerShell.Security/About/about_Certificate_Provider.md).
313+
For more information about the Certificate provider and the `Cert:` drive, see
314+
[about_Certificate_Provider](../Microsoft.PowerShell.Security/About/about_Certificate_Provider.md).
352315

353316
### Example 8: Get items using the Depth parameter
354317

@@ -439,26 +402,27 @@ For example, to get non-system files (not directories) that are encrypted or com
439402
To find files and folders with commonly used attributes, use the **Attributes** parameter. Or, the
440403
parameters **Directory**, **File**, **Hidden**, **ReadOnly**, and **System**.
441404

442-
The **Attributes** parameter supports the following properties:
443-
444-
- **Archive**
445-
- **Compressed**
446-
- **Device**
447-
- **Directory**
448-
- **Encrypted**
449-
- **Hidden**
450-
- **IntegrityStream**
451-
- **Normal**
452-
- **NoScrubData**
453-
- **NotContentIndexed**
454-
- **Offline**
455-
- **ReadOnly**
456-
- **ReparsePoint**
457-
- **SparseFile**
458-
- **System**
459-
- **Temporary**
460-
461-
For a description of these attributes, see the [FileAttributes Enumeration](/dotnet/api/system.io.fileattributes).
405+
The **Attributes** parameter supports the following values:
406+
407+
- `Archive`
408+
- `Compressed`
409+
- `Device`
410+
- `Directory`
411+
- `Encrypted`
412+
- `Hidden`
413+
- `IntegrityStream`
414+
- `Normal`
415+
- `NoScrubData`
416+
- `NotContentIndexed`
417+
- `Offline`
418+
- `ReadOnly`
419+
- `ReparsePoint`
420+
- `SparseFile`
421+
- `System`
422+
- `Temporary`
423+
424+
For a description of these attributes, see the [FileAttributes](xref:System.IO.FileAttributes)
425+
enumeration.
462426

463427
To combine attributes, use the following operators:
464428

@@ -754,7 +718,7 @@ Accept wildcard characters: False
754718

755719
Allows the cmdlet to get items that otherwise can't be accessed by the user, such as hidden or
756720
system files. The **Force** parameter doesn't override security restrictions. Implementation varies
757-
among providers. For more information, see
721+
by provider. For more information, see
758722
[about_Providers](../Microsoft.PowerShell.Core/About/about_Providers.md).
759723

760724
```yaml
@@ -829,7 +793,8 @@ typed. No characters are interpreted as wildcards. If the path includes escape c
829793
it in single quotation marks. Single quotation marks tell PowerShell to not interpret any characters
830794
as escape sequences.
831795

832-
For more information, see [about_Quoting_Rules](../Microsoft.Powershell.Core/About/about_Quoting_Rules.md).
796+
For more information, see
797+
[about_Quoting_Rules](../Microsoft.PowerShell.Core/About/about_Quoting_Rules.md).
833798

834799
> [!NOTE]
835800
> The **Include** and **Exclude** parameters have no effect when used with the **LiteralPath**
@@ -867,8 +832,9 @@ Accept wildcard characters: False
867832

868833
### -Path
869834

870-
Specifies a path to one or more locations. Wildcards are accepted. The default location is the
871-
current directory (`.`).
835+
Specifies a path to one or more locations. If not specified, the default location is the current
836+
directory (`.`). Wildcards are accepted. Use care when using the **Path** parameter with the
837+
**Recurse** parameter. For more information, see the [NOTES](#notes) section of this article.
872838

873839
```yaml
874840
Type: System.String[]
@@ -1047,20 +1013,41 @@ Windows PowerShell includes the following aliases for `Get-ChildItem`:
10471013
- `dir`
10481014
- `gci`
10491015

1050-
`Get-ChildItem` doesn't get hidden items by default. To get hidden items, use the **Force**
1051-
parameter.
1016+
**Recursion behavior with the Path parameter:**
10521017

1053-
The `Get-ChildItem` cmdlet is designed to work with the data exposed by any provider. To list the
1054-
providers available in your session, type `Get-PSProvider`. For more information, see
1055-
[about_Providers](../Microsoft.PowerShell.Core/About/about_Providers.md).
1018+
When you use `Get-ChildItem -Recurse` with the **Path** parameter, the cmdlet searches for the last
1019+
path component whether or not it's a wildcard pattern or a literal name.
1020+
1021+
- If the last path component matches an existing immediate subdirectory of the target directory, the
1022+
cmdlet performs recursive enumeration on the matching directory.
1023+
- If the last path component doesn't match an existing immediate subdirectory of the target
1024+
directory, the cmdlet recursively searches the target directory's hierarchy for items that match
1025+
the last path component
1026+
1027+
When you use `Get-ChildItem -Recurse` with the **Path** and **Name** parameters, the behavior
1028+
changes. The command searches for the last path component among the immediate child items of the
1029+
target directory.
1030+
1031+
- If there is a match among the immediate child items, the cmdlet performs recursive enumeration on
1032+
the matching items. Wildcard matching happens only once, at the top level of the target directory.
1033+
The results are processed as if they had been individually passed to the **LiteralPath**
1034+
parameter.
1035+
- If the last path component doesn't match any items at the top level, an error occurs.
1036+
1037+
You should avoid using the **Path** parameter with the **Recurse** parameter. For best results:
1038+
1039+
- Use **LiteralPath** to specify the target directory to avoid triggering a recursive search for the
1040+
last path component.
1041+
- Use **Filter** or **Include** parameters to specify wildcard or literal patterns that should be
1042+
searched for in every level of the target directory's hierarchy.
10561043

10571044
## RELATED LINKS
10581045

10591046
[about_Certificate_Provider](../Microsoft.PowerShell.Security/About/about_Certificate_Provider.md)
10601047

10611048
[about_Providers](../Microsoft.PowerShell.Core/About/about_Providers.md)
10621049

1063-
[about_Quoting_Rules](../Microsoft.Powershell.Core/About/about_Quoting_Rules.md)
1050+
[about_Quoting_Rules](../Microsoft.PowerShell.Core/About/about_Quoting_Rules.md)
10641051

10651052
[about_Registry_Provider](../Microsoft.PowerShell.Core/About/about_Registry_Provider.md)
10661053

0 commit comments

Comments
 (0)