As I mentioned in question, this seems like bug in Powershell engine.
When I try to print the files in the current directory(where the said powershell script is also present) by excluding some file-types(extensions), it doesn't print anything. For eg. below codes print contents of Current Folder in the Powershell console:
gci
gci -pa .
Both the above codes print the directory contents as below:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 20-09-2020 22:37 835799796 file1.mkv
-a---- 20-09-2020 22:25 148 file1.srt
-a---- 23-09-2020 04:53 357 scriptv1.ps1
-a---- 20-09-2020 22:25 678 file1.txt
But when I run below code, it doesn't print anything, when it must print file1.txt:
$excluded = @('*.mkv','*.mp4','*.srt','*.sub','*.ps1')
Get-ChildItem -Exclude $excluded | Write-Host { $_.FullName }
Can anyone assist figuring out why is it happening and how to get what I mentioned ?
The use of
-Exclude
withGet-ChildItem
is not intuitive. To get consistent results withGet-ChildItem
, you must qualify your path with\*
or use the-Recurse
switch. If you do not care to recurse, you can just useGet-Item
with a\*
qualifying the path.The reason recursion should be used is because
-Exclude
values are applied to the leaf of-Path
value first. If any of those exclusions match your target directory, then it will be excluded and prevent any of its items from being displayed. See below: