Search code examples
powershellpowershell-2.0

Find all files with the exact extension in Powershell


I'm trying to get all the files that end with ".asp" but I also get the files that ends in ".aspx" or ".asp..." when I do :

Get-ChildItem C:\test -Filter *.asp -Recurse | % {$_.FullName}

For the example, let's say I have a directory with test1.asp, test2.aspx and test3.asp, if I execute my script, the output will be:

C:\test\test1.asp
C:\test\test2.aspx
C:\test\test3.asp

but I only wanted it to get me "test1.asp and test3.asp". For information, I use Powershell 2.1.

Can someone tell me how to fix that?


Solution

  • Try to check one more the last 3 symbols

    Get-ChildItem 'C:\test' -Filter '*.asp' -Recurse |
    Where {$_.Name.substring($_.Name.length -3, 3)  -Match 'asp'} | % {$_.FullName}