Powershell get number of files and folders in directory

12,003

Solution 1

Well, you want two different things, so you're probably going to need to run two different commands. Count folders:

(GCI|?{$_.PSIsContainer}).Count

And then count files:

(GCI|?{!$_.PSIsContainer}).Count

Solution 2

You can do that using the following command:

$headers = @{ $true='Folder'; $false='File' }
Get-ChildItem |
  Group-Object PSIsContainer |
  Select-Object @{ Name="Type"; Expression={ $headers[$_.Name -eq $true] } }, Count

Or all in one line:

gci | group psiscontainer | select @{n="Type";e={@{$true='Folder';$false='File'}[$_.Name -eq $true]}}, Count
Share:
12,003
NebDaMin
Author by

NebDaMin

Software Developer from the Midwest of USA.

Updated on June 04, 2022

Comments

  • NebDaMin
    NebDaMin almost 2 years

    I am going to write a powershell script that gets various statistics on the dir it is in.

    If I do

    get-childitem
    

    I get this

    d----        06/23/2014   2:49 PM            asdf;
    -a---        06/23/2014   2:49 PM         23 New Text Document - Copy (2).txt
    -a---        06/23/2014   2:49 PM         23 New Text Document - Copy (3).txt
    -a---        06/23/2014   2:49 PM         23 New Text Document - Copy (4).txt
    -a---        06/23/2014   2:49 PM         23 New Text Document - Copy (5).txt
    -a---        06/23/2014   2:49 PM         23 New Text Document - Copy.txt
    -a---        06/23/2014   2:49 PM         23 New Text Document.txt
    

    And if I do

    (get-childitem).count
    

    I get

    7
    

    I want to be able to count the number of files in the directory as well as the number of folders. Any suggestions would be appreciated. thanks.

  • Mike Shepard
    Mike Shepard almost 10 years
    Simplified it a little...Get-ChildItem | group @{Expression={if($_.PSISContainer){'Folder'} else {'File'}}} -NoElement