thanks to your advices, I have done a recursive function. here it is, I know it's not perfect, but if someone gets to have the same problem, here what worked for me
function Recursive-FolderSearch {
param (
[string[]]$Paths,
[string]$expiredDate
)
foreach ($Path in $Paths) {
if ((Test-Path $Path -PathType Container) -and (Split-Path $Path -Leaf) -match '^\d{4}-\d{2}$') {
$folderDays = Get-ChildItem -Path $Path -Attributes Directory
foreach ($folderDay in $folderDays) {
if ($folderDay.Name -match '^\d{2}$') {# if folder respect format DD
# concat folder AAAA-MM and folder format DD to get complete date
$dateDossier = [datetime]::ParseExact("$(Split-Path $Path -Leaf)-$($folderDay.Name)", "yyyy-MM-dd", $null)
if ($dateDossier -lt $expiredDate) { #compare folder date with actual date
# then suppress folder if older than requested
Remove-Item -Path $folderDay.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
#Supprimer le dossier AAAA-MM s'il est vide
if (-not (Get-ChildItem -Path $Path -Force -Recurse -ErrorAction SilentlyContinue )) { #| Where-Object { $_. PSIsContainer -eq $true }
Remove-Item -Path $Path -Force -Recurse -ErrorAction SilentlyContinue
}
}
elseif (Test-Path $Path -PathType Container) {
$subDirs = Get-ChildItem -Path $Path -Attributes Directory -Force
foreach ($subDir in $subDirs) {
Recursive-FolderSearch -Paths $subDir.FullName $expiredDate
}
}
}
}