Powershell function - No result

Hi,

Can you help me to find what is wrong in my powershell function ?

If I execute this request :

Get-ChildItem -Recurse -Path C:\tests_onedrive\travail -Include *.bmp,*.jpg | Select-Object DirectoryName -Unique

I get this result :

PS C:\tests_onedrive\Script> Get-ChildItem -Recurse -Path C:\tests_onedrive\travail -Include *.bmp,*.jpg | Select-Object DirectoryName -Unique

DirectoryName

C:\tests_onedrive\travail\ANGELA\SON\XX
C:\tests_onedrive\travail\ANGELA\SON
C:\tests_onedrive\travail

PS C:\tests_onedrive\Script>

I’m trying to do a function that do the same. Actually, my function is :

#Variables for Processing $SearchFolder="C:\tests_onedrive\travail" $FileType="*.jpg,*.bmp"

Function get directory that contain specific files type

Write-Host $SearchFolder
Function List-Directory-With ($SearchFolderLDW, $FileTypeLDW) {
$FolderWith = Get-ChildItem -Recurse -Path $SearchFolderLDW -Include $FileTypeLDW | Select-Object DirectoryName -Unique
Return $FolderWith
}

$Res = List-Directory-With $SearchFolder $FileType
Write-Host $Res

The result of this after execution is … no result :

PS C:\tests_onedrive\Script> #Variables for Processing $SearchFolder="C:\tests_onedrive\travail" $FileType="*.jpg,*.bmp"

Function get directory that contain specific files type

Write-Host $SearchFolder
Function List-Directory-With ($SearchFolderLDW, $FileTypeLDW) {
$FolderWith = Get-ChildItem -Recurse -Path $SearchFolderLDW -Include $FileTypeLDW | Select-Object DirectoryName -Unique
Return $FolderWith
}

$Res = List-Directory-With $SearchFolder $FileType
Write-Host $Res
C:\tests_onedrive\travail

PS C:\tests_onedrive\Script>

Can you help me to find what is wrong ?
Thanks, Regards,

Change line 3 to say:

$FileType='*.jpg','*.bmp'

You’re getting no results because there’s no files with extension .jpg*.bmp

Hi Olivier,
The problem is not so much the function, but more the values that are being provided to Get-ChildItem.

If you look at the -Include parameter using Get-Help Get-ChildItem -Full, you will see that it is expecting an arrary -Include ‹string›[]

When you ran the below for the -Include you passed in an array with 2 string element

Get-ChildItem -Recurse -Path C:\tests_onedrive\travail -Include *.bmp,*.jpg | Select-Object DirectoryName -Unique

However here you are creating a single string $FileType=".jpg,.bmp", and Get-ChildItem correctly finds no files with “.jpg,.bmp” in the path.

You should correct your variable to have an array and each element include your include pattern string.

IE.

$FileType="*.jpg","*.bmp"