Here is a pretty simple script that should do the trick... Keep in mind that it will append the data if there is already an existing CSV. If you only plan on running the script once, that shouldn't be a problem. If you plan to run this on a schedule, I would add some logic in to create a uniquely named CSV each time it is run.
$DirectoriesFile = 'C:\DirectoriesList.txt' ##Replace with the path to your file with directories
$CSV = 'C:\Data.csv' ##Replace with the path to where the CSV should be output
Add-Content -Path $CSV -Value 'Directory,File name'
foreach($line in Get-Content -Path $DirectoriesFile) {
$sourceFile=gci $line | sort LastWriteTime | select -Last 1
$fileName = Split-Path $sourceFile -leaf
$directory = $line.Split("/")[-1]
Add-Content -Path $CSV -Value $directory','$fileName
}
Your post implies that only one file will exist in each directory. I added a filter to ensure that only one is added to the CSV within each directory (it uses the last write time). If you are expecting multiple files within the directories you are searching, this code below will work better.
$DirectoriesFile = 'C:\DirectoriesList.txt' ##Replace with the path to your file with directories
$CSV = 'C:\Data.csv' ##Replace with the path to where the CSV should be output
Add-Content -Path $CSV -Value 'Directory,File name'
foreach($line in Get-Content -Path $DirectoriesFile) {
$directory = $line.Split("\")[-1]
foreach($fileName in gci $line){
Add-Content -Path $CSV -Value $directory','$fileName
}
}