NebDaMin
NebDaMin

Reputation: 658

Powershell get number of files and folders in directory

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.

Upvotes: 3

Views: 13280

Answers (2)

Yevgeniy.Chernobrivets
Yevgeniy.Chernobrivets

Reputation: 3194

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

Upvotes: 4

TheMadTechnician
TheMadTechnician

Reputation: 36277

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

Upvotes: 5

Related Questions