Powershell - Create file from foreach when file counts greater than 250

tecgate
tecgate used Ask the Experts™
on
$fso = New-Object -com "Scripting.FileSystemObject"
$f = $fso.GetFolder("D:\websphere\logs")

Open in new window


I have a directory called D:\websphere\logs, and D:\websphere\logs has many of sub-directories.   Each sub-directory doesn't any child sub-directory, so  the directory structure is:

D:\websphere\logs
       \aaa
       \bbb
       \ccc
       \ddd
       etc etc

I need to count the files under each sub-directory, and write to a .txt file when the file count of each sub-folder is greater than 250.

I have the script below, but not working really.

foreach ($folder in $f.subfolders)
          {
             Write-Host $((get-childitem $folder.path).count)','$folder.Path

  New-Object -TypeName PSObject -Property @{
                SystemName = $((get-childitem $folder.path).count)
                Reachable = $folder.Path
  
   } | Export-Csv -path D:\websphere\output.txt -Append  

$data = import-csv -path D:\websphere\output.txt -header close, server
$data | where-object{$_.close -gt 250} |format-table -autosize

Open in new window


The count is working fine, but I can't make the next piece (write file count to a .txt file when the file count of each sub-folder is greater than 250).

So, my working segment so far is:

$fso = New-Object -com "Scripting.FileSystemObject"
$f = $fso.GetFolder("C:\Windows\Media")
foreach ($folder in $f.subfolders){Write-Host $((get-childitem $folder.path).count)','$folder.Path}

Open in new window



Please let me know how to make this piece (write file count to a .txt file when the file count of each sub-folder is greater than 250) working.

Appreciate it!
Comment
Watch Question
Show All Comments

Welcome back to Experts Exchange!

You've reached the end of your complimentary solution views. To submit your own question to Ask the Experts™ or to view more solutions, start a free trial of Expert Office®.

Start 7-Day Free Trial

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial