Question:
I am now better in writing, stealing and modifying scripts but here I am a stuck a little.
My script sums up files in a specific folder and counts them.
Like this :
1 2 3 4 5 6 7 8 9 10 |
{ function alljournals { $colItems = (get-childitem "$targetfolder" -include *.xlsx* -recurse | measure-object -property length -sum) write-host "Collected all .xlsx files from all ImportJournal folders" -foregroundcolor "green" "Number of files: "+ $colItems.count + [Environment]::NewLine + "Size of all files "+"{0:N2}" -f ($colItems.sum / 1MB) + " MB" } alljournals } |
What’s the smartest way to list all files from that folder using write-host? I tried to put a write-host $colitems.Fullname without good results.
I know that I could easily put the files in a loop and than use foreach and display the filename. But for that I have to change everything. So is there a way?
Right now it displays something like that :
1 2 3 4 |
Collected all .xlsx files from all Importjournalfolders Number of files 15 Size of all files: 13,3mb |
What I need before that output is a list of all files with path
Any Ideas or tips ?
Answer:
You could capture the list of files in the first pipeline using tee-object, and then use foreach on that.
1 2 3 |
$colItems = (get-childitem "$targetfolder" -include *.xlsx* -recurse | tee-object -variable files | measure-object -property length -sum) $files | foreach-object {write-host $_.FullName} |