How do I filter a string array (or list) in PowerShell using the 'Match' cmdlet?
The -match operator is both a comparison operator and an array operator, depending on its input object.
If it's a scalar, it returns a boolean. If it's an array, it returns all the elements of the array that match the pattern
@($Filenames) -match '*.csv'
Use the array syntax to ensure that you still get an array if there's only one filename returned by Get-ChildItem
. Otherwise, you'll get back $True
instead of the filename if it matches.
Try this:
$FileNames = Get-ChildItem -Path "C:\Users\anagre\Desktop" -Filter *.csv
In your above code you didn't use the $PSItem ($_) in your where
clause, and if you want to use a wildchar you have got to use the -like
operator:
$FileNames|where{$_ -like "*.csv"}
or
$FileNames|where{$_ -match ".csv"}