Home > Enterprise >  I want to import a csv to use as filter in the powershels gci command
I want to import a csv to use as filter in the powershels gci command

Time:01-19

I have a csv file with extension and description. I want to import that file and use it as the filter parameter in a gci command. But I get no results.

I expect to get a list of the jpg files but get no results.

$extensions=Import-CSV -Path c:\scripts\Media-extension-foto.csv 
#$extensions=Import-CSV -Path c:\scripts\Media-extension-foto.csv -header extension

$extensions.extension

$src = "c:\scripts\"
#gci c:\scripts\ -Include $Extensions.extension #-Force -recurse
#gci c:\scripts\ -filter $Extensions.extension #-Force -recurse
gci c:\scripts\|where{$_ -like $extensions.extension}`

my csv file looks like this (just made a small file for testing)

extension,"description"
*.JPEG,JPEG Image
*.JPF,JPEG 2000 Image
*.JPG,JPEG Image
*.JPG_LARGE,Twitter Large JPEG Image

There are jpg files in that folder :

    Directory: C:\Scripts

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          23/11/2022    11:02         509592 nieuw9754560_02-10.jpg
-a---          23/11/2022    11:02         576486 nieuw9754560_02-15.jpg
-a---          23/11/2022    11:02         641802 nieuw9754560_02-20.jpg
-a---          23/11/2022    11:01         705702 nieuw9754560_02-25.jpg
-a---          23/11/2022    11:01         763249 nieuw9754560_02-30.jpg

CodePudding user response:

I've just tested this - I think all you're missing is changing the $_ to $_.Extension for the Get-ChildItem, on the last line.

Hope that helps.

CodePudding user response:

Doesn't directly fix the code but here's another way of getting the result using foreach to iterate through each extension in the array:

foreach($e in $extensions.extension) {
 gci c:\scripts\ | where {$_ -like $e} 
}
  • Related