138

I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (c:\MyFolder). Here's what I've done:

Write-Host ( Get-ChildItem c:\MyFolder ).Count;

The problem is, that if I have 1 or 0 items, the command does not work---it returns nothing.

Any ideas?

1
  • 1
    This is really old, so here's the quick answer for the old problem: Change your command to Write-Host @( Get-ChildItem c:\MyFolder ).Count;. This keeps older versions of Powershell from collapsing/unwrapping the list/array returned by Get-ChildItem when there's less than 2 elements in it. Commented Nov 17, 2021 at 19:18

8 Answers 8

219

You should use Measure-Object to count things. In this case it would look like:

Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count;

or if that's too long

Write-Host ( dir c:\MyFolder | measure).Count;

and in PowerShell 4.0 use the measure alias instead of mo

Write-Host (dir c:\MyFolder | measure).Count;
Sign up to request clarification or add additional context in comments.

3 Comments

How to get count for all folders inside a particular folder?
THis Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count worked for me
The measure alias replaces mo starting from PowerShell 4.0
54

If you need to speed up the process (for example counting 30k or more files) then I would go with something like this..

$filepath = "c:\MyFolder"
$filetype = "*.txt"
$file_count = [System.IO.Directory]::GetFiles("$filepath", "$filetype").Count

4 Comments

That is an order of magnitude faster! Thanks. I had a folder with over 29K files in it, and this method returned the count as fast as my finger lifted from the RETURN key.
You can recurse this with [System.IO.Directory]::GetFiles("$filepath", "$filetype",1) See here
Or filter with regex with $r = <regex> and (Get-ChildItem) -match $r | Select FullName, @{name = "FileCount"; expression = {[System.IO.Directory]::GetFiles($_).Count}}
Please note! GetFiles mentioned here uses the flawed search method (like Get-ChildItem -Filter does) that will match *.bmp2 when searching for *.bmp. This means searching for *.bmp is effectively using a search pattern of *.bmp* - which can easily cause issues.
50

I finally found this link:

https://blogs.perficient.com/microsoft/2011/06/powershell-count-property-returns-nothing/

Well, it turns out that this is a quirk caused precisely because there was only one file in the directory. Some searching revealed that in this case, PowerShell returns a scalar object instead of an array. This object doesn’t have a count property, so there isn’t anything to retrieve.

The solution -- force PowerShell to return an array with the @ symbol:

Write-Host @( Get-ChildItem c:\MyFolder ).Count;

1 Comment

Perfect, thanks. I find it annoying when commandlets try and be "helpful" and vary their return type.
22

Only Files

Get-ChildItem D:\ -Recurse -File | Measure-Object | %{$_.Count}

Only Folders

Get-ChildItem D:\ -Recurse -Directory | Measure-Object | %{$_.Count}

Both

Get-ChildItem D:\ -Recurse | Measure-Object | %{$_.Count}

3 Comments

On the first one, how would I search for all jpg and png files?
@AlLelopath Get-ChildItem D:\ -Recurse -File -Include *.jpg,*.png | Measure-Object | %{$_.Count}
hi @dhcgn any advice on this stackoverflow.com/questions/54916917/…
13

You can also use an alias

(ls).Count

1 Comment

This is exactly what I was looking for
4

Recursively count files in directories in PowerShell 2.0

ls -rec | ? {$_.mode -match 'd'} | select FullName,  @{N='Count';E={(ls $_.FullName | measure).Count}}

Comments

1

In powershell you can to use severals commands, for looking for this commands digit: Get-Alias;

So the cammands the can to use are:

write-host (ls MydirectoryName).Count

or

write-host (dir MydirectoryName).Count

or

write-host (Get-ChildrenItem MydirectoryName).Count

Comments

1

To count the number of a specific filetype in a folder. The example is to count mp3 files on F: drive.

( Get-ChildItme F: -Filter *.mp3 - Recurse | measure ).Count

Tested in 6.2.3, but should work >4.

1 Comment

( Get-ChildItem .\ -Filter *.mp3 -Recurse | measure ).Count

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.