Search code examples
powershellget-childitem

Using the -Depth property of the Get-ChildItem Powershell cmdlet


I'm trying to use the -Depth parameter of the Get-Children cmdlet to find the shallower (less deep) of 2 files having the same name as shown below.

C:\temp\test.txt
C:\temp\Logs\test.txt

Many posts suggest defining -Path as "C:\temp\" or "C:\temp\\*". But in my case I prefer to use the -Depth Parameter to limit the depth of recursions in the search. I've read that it implies recursion and thus need not be used in conjunction with recurse. So far I've tried all the commands below but they all return the same results shown further down.

Get-ChildItem -Path C:\temp -Depth 0 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 1 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 2 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 3 -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth '1' -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth "1" -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth $d -Include tes*.txt | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth $d -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 0 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 0 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 2 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Depth 3 -Include tes*.txt -Recurse | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Include tes*.txt -Depth 1 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -Include tes*.txt -Depth 0 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -File -Include tes*.txt -Depth 0 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -File -Include tes*.txt -Depth 1 | Format-List -Property FullName
Get-ChildItem -Path C:\temp -File -Include tes*.txt -Depth 2 | Format-List -Property FullName
Get-ChildItem -Path C:\temp\* -Include tes*.txt -Recurse | Format-List -Property FullName

ALL of the commands above yield the same result, namely

FullName : C:\temp\Logs\test.txt
FullName : C:\temp\test.txt

Aside from the -Depth property, using the "\*" as many suggest enables me to isolate the deeper file but not the shallower file. Am I missing something?

PS C:\>  Get-ChildItem -Path C:\temp\* -Include tes*.txt -Recurse | Format-List -Property FullName

FullName : C:\temp\Logs\test.txt
FullName : C:\temp\test.txt

PS C:\>  Get-ChildItem -Path C:\temp\*\* -Include tes*.txt -Recurse | Format-List -Property FullName

FullName : C:\temp\Logs\test.txt

PS C:\>  Get-ChildItem -Path C:\temp\*\*\* -Include tes*.txt -Recurse | Format-List -Property FullName

PS C:\> 

Solution

  • The usage of -Depth seems to exclude the usage of -Include or
    even a wildcard in the -Path parameter.

    Let -Filter do the work, in this sample tree:

    > tree /F
    C:.
    └───temp
        │   Test.txt
        │
        └───0
            │   Test.txt
            │
            └───1
                │   Test.txt
                │
                └───2
                        Test.txt
    

    This one liner:

     0..4|%{"-Depth $_ ---------------";(Get-ChildItem -Path C:\Temp\ -Depth $_ -Filter Tes*.txt).FullName}
    

    returns:

    -Depth 0 ---------------
    C:\Temp\Test.txt
    -Depth 1 ---------------
    C:\Temp\Test.txt
    C:\Temp\0\Test.txt
    -Depth 2 ---------------
    C:\Temp\Test.txt
    C:\Temp\0\Test.txt
    C:\Temp\0\1\Test.txt
    -Depth 3 ---------------
    C:\Temp\Test.txt
    C:\Temp\0\Test.txt
    C:\Temp\0\1\Test.txt
    C:\Temp\0\1\2\Test.txt
    -Depth 4 ---------------
    C:\Temp\Test.txt
    C:\Temp\0\Test.txt
    C:\Temp\0\1\Test.txt
    C:\Temp\0\1\2\Test.txt