1

I am trying to use powershell to produce a list of folder names and how many files are in each folder.

I have this script

$dir = "C:\Users\folder" 
Get-ChildItem $dir -Recurse -Directory | ForEach-Object{
    [pscustomobject]@{
        Folder = $_.FullName
        Count = @(Get-ChildItem -Path $_.Fullname -File).Count
    }
} | Select-Object Folder,Count

Which lists the file count, but it puts the full path (i.e. C:\Users\name\Desktop\1\2\-movi...). Is there any way to just display the last folder ("movies") as well as save the result to a .txt file?

Thank you

jwodder
  • 54,758
  • 12
  • 108
  • 124
Mike P
  • 13
  • 4

2 Answers2

1

Instead of $_.FullName, use $_.Name to only get the directory name.

Your Select-Object call is redundant - it is effectively a no-op.

While it's easy to send the results to a .txt file with >, for instance, it's better to use a more structured format for later programmatic processing. In the simplest form, that means outputting to a CSV file via Export-Csv; generally, however, the most faithful way of serializing objects to a file is to use Export-CliXml.

Using Export-Csv for serialization:

$dir = 'C:\Users\folder'
Get-ChildItem -LiteralPath $dir -Recurse -Directory | ForEach-Object {
    [pscustomobject] @{
      Folder = $_.Name
      Count = @(Get-ChildItem -LiteralPath $_.Fullname -File).Count
    }
} | Export-Csv -NoTypeInformation results.csv

Note that you could streamline your command by replacing the ForEach-Object call with a Select-Object call that uses a calculated property:

$dir = 'C:\Users\folder'
Get-ChildItem -LiteralPath $dir -Recurse -Directory |
  Select-Object Name,
    @{ n='Count'; e={@(Get-ChildItem -LiteralPath $_.Fullname -File).Count} } |
      Export-Csv -NoTypeInformation results.csv
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • My pleasure, @MikeP. Please see my update for how you could streamline your command. Generally, use the `Get-Member` cmdlet to inspect a command's output types, so you can discover the full type name and available properties / methods. – mklement0 Dec 01 '18 at 03:32
0

You mean something like this...

Clear-Host
Get-ChildItem -Path 'd:\temp' -Recurse -Directory | 
Select-Object Name,FullName,
@{Name='FileCount';Expression = {(Get-ChildItem -Path $_.FullName -File -Recurse| Measure-Object).Count}} `
| Format-Table -AutoSize

# Results


Name           FullName                               FileCount
----           --------                               ---------
abcpath0       D:\temp\abcpath0                               5
abcpath1       D:\temp\abcpath1                               5
abcpath2       D:\temp\abcpath2                               5
Duplicates     D:\temp\Duplicates                         12677
EmptyFolder    D:\temp\EmptyFolder                            0
NewFiles       D:\temp\NewFiles                               4
PngFiles       D:\temp\PngFiles                               4
results        D:\temp\results                              905
...
postanote
  • 15,138
  • 2
  • 14
  • 25