Find all files inside directory Trees with a given name. Powershell question
I need help understanding the cmdlet behavior.
Simple command Get-ChildItem
I want to find all the files in directories with a given name.
Example Data:
c:\temp\AD\File1.txt
c:\temp\AD\File2.txt
c:\temp\AD\File3.txt
c:\temp\AD\FileX.txt
c:\temp2\AD\File1.txt
c:\temp2\AD\File2.txt
Set-location c:\
Get-ChildItem -Path "*\AD\*" -Include *.txt -Recurse
This command will run correctly and show file1 through FileX in any part of the directory tree.
Change the Set-location to C:\temp and no files displayed. Even though the AD directory exists in the tree.
The problem is when I'm looking for files in a directory on the root of the Get-ChildItem -Path "*\$myDriectoryName\*" -Include *.txt -Recurse will not display anything.
I can use the where-object to filter but I'm curious about the behavior. Can I write the path in a different way?
This is the updated code
Set-location c:\ Get-ChildItem -Include *.txt -Recurse | where {$_.FullName -like "*\AD\*"}
you don't need to use the path if you already use set-location.
the update here is adding the where filter to filter all the stream from the Get-ChildItem and check if in the full path of the result include a folder named AD.
as you are scanning the entire root this should give you the result you are looking for.