Have a folder that has another folder, and that folder having a file.
\foo
\foo\bar
\foo\bar\foo bar.txt
Try to use the following to find the TXT file.
get-childitem -file '\foo\*a*' -recurse
an object returned that contains foo bar.txt
This is expected because -file should only restrict the output, not the input
no object is instead returned
It appears that the input wildcard path is being filtered by -file before -recurse can occur. If at least one file matches, then the recursion will occur. So now create file \foo\bar.txt, and the expected behavior will occur, but also \foo\bar.txt also appears in the result object (it does match the wildcard).
Now add \foo\foo and copy \foo\bar.txt to \foo\foo. This location of the file will also be included, indicating that the wildcard in the path is only restricting the output, not the available folders in recursion. Remove \foo\bar.txt and the problem returns of no output.
I am considering this an issue as it relates to the fact that path input to Get-ChildItem could have been supplied by someone less familiar with PowerShell and Get-ChildItem, as they were provided the script, or the script simply provides a prompt. It otherwise works fine if wildcards are not included in -path, but instead in -filter or -include.
> $PSVersionTable
Name Value
---- -----
PSVersion 6.0.3
PSEdition Core
GitCommitId v6.0.3
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
I ran in to this, because I was trying to get a listing of all files without an extension:
dir *. -file
doesn't work. CMD 'dir /a-d *.' recognizes this as get only files without extensions. However I did catch it find a file '.sou', or other files and folders that start with a '.' and don't have any additional '.'s.
dir -file -filter *.
does work as did this:
dir .\* -file -exclude *.*
if you just try:
dir *.
it works except it includes directories.
I think this is a related issue. The -File and -Exclude parameters don't work when used together.
Count of files without any exclusions
PS C:\Program Files\PowerShell\6.0.4> (Get-ChildItem).count
389
Count of files when excluding pwsh.exe
PS C:\Program Files\PowerShell\6.0.4> (Get-ChildItem -Exclude pwsh.exe).count
388
Count of files when excluding pwsh.exe and only listing files
PS C:\Program Files\PowerShell\6.0.4> (Get-ChildItem -Exclude pwsh.exe -file).count
0
Most helpful comment
I think this is a related issue. The -File and -Exclude parameters don't work when used together.
Count of files without any exclusions
PS C:\Program Files\PowerShell\6.0.4> (Get-ChildItem).count389Count of files when excluding pwsh.exe
PS C:\Program Files\PowerShell\6.0.4> (Get-ChildItem -Exclude pwsh.exe).count388Count of files when excluding pwsh.exe and only listing files
PS C:\Program Files\PowerShell\6.0.4> (Get-ChildItem -Exclude pwsh.exe -file).count0