Is there any reason you are trying to do this using .NET functions instead of just using the built in PS cmdlets?
Get-ChildItem $folderPath -include *.tar -recurse | Select-Object -First 4
That should give you the first 4 files that match the given extension, recursively searching the given folder path.
But if you want to use LINQ directly then you can, it's just more complex.
[System.IO.Directory]::EnumerateFiles($folderPath, '*.tar', [System.IO.SearchOption]::AllDirectories) | Select-Object -First 4
Note that Take
is an extension method and cannot be called on the object returned by EnumerateFiles
. It is effectively a static method. In C# we can call it using instance method syntax but that is a C# thing only.
[System.Linq.Enumerable]::Take([System.IO.Directory]::EnumerateFiles($folderPath, '*.tar', [System.IO.SearchOption]::AllDirectories), 4)