1

Why Get-ChildItem in Powershell is returning files not matching my filter in some cases?

PS C:\Users\JairoAndres> Get-ChildItem -Path C:\Hyper-V\Disks\ -Filter "*.vh"
PS C:\Users\JairoAndres> Get-ChildItem -Path C:\Hyper-V\Disks\ -Filter "*.vhd"


    Directorio: C:\Hyper-V\Disks


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----    18/07/2019  10:17 a. m.       75497472 DockerDesktop.vhdx
-a----     18/07/2019  3:10 p. m.    64361594880 WinDev.vhdx

Look at those files ".vhdx" not matching *.vhd. Having into account that when I use a filter like *.vh no file yielded

  • 1
    Quoting the critical part of this answer - https://stackoverflow.com/a/12914655/3156906 - "Another gotcha to be kept in mind: ls -Filter ```*.txt``` in fact gets files like ```*.txt*```". So in your case, ```*.vhd``` => ```*.vhd*``` and includes your ```*.vhdx``` files. – mclayton Nov 09 '19 at 21:14
  • 1
    Unfortunately -filter also matches on the short version of files that can be seen with `cmd /c dir /x`. – js2010 Nov 10 '19 at 16:08
  • @mclayton that's not true, that's why I put the example of `*.vh` not yielding any result. – Jairo Andres Velasco Romero Nov 12 '19 at 17:09
  • @JairoAndresVelascoRomero - if there are 3 characters in the extension (e.g. ```*.vhd```) it treats it like there's a wildcard on the end (e.g. ```*.vhd*```), so ```*.vhd``` matches ```*.vhdx```, whereas if there are only two characters it treats it literally - i.e. ```*.vh``` searches for ```*.vh```, and does **not** match ```*.vhdx```. It's to do with backward compatibility with MSDOS search expressions as per the other answer I linked to. – mclayton Nov 12 '19 at 19:48

0 Answers0