Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File counting with Powershell commands

How can I count all files in a specific folder (and all subfolders) with the Powershell command Get-ChildItem? With (Get-ChildItem <Folder> -recurse).Count also the folders are counted and this is not that what I want. Are there other possibilities for counting files in very big folders quickly?

Does anybody know a short and good tutorial regarding the Windows Powerhell?

like image 772
Elmex Avatar asked Nov 23 '11 09:11

Elmex


People also ask

How do I get a count of files?

To determine how many files there are in the current directory, put in ls -1 | wc -l. This uses wc to do a count of the number of lines (-l) in the output of ls -1. It doesn't count dotfiles.

How do you use the count function in PowerShell?

The PowerShell Count Operator of an object can be accessed by wrapping the object in a bracket (). Then, add a period (.), followed by the word, count.

What PowerShell command will allow for counting lines in a file?

To count the total number of lines in the file in PowerShell, you first need to retrieve the content of the item using Get-Content cmdlet and need to use method Length() to retrieve the total number of lines.

How do I count the number of files in a folder?

Right-click on the folder and select the “Properties” option. The “Properties” window will open and you will be able to see the number of files and subdirectories located in the directory selected.


2 Answers

I would pipe the result to the Measure-Object cmdlet. Using (...).Count can yield nothing in case there are no objects that match your criteria.

 $files = Get-ChildItem <Folder> -Recurse | Where-Object {!$_.PSIsContainer} | Measure-Object
 $files.Count

In PowerShell v3 we can do the following to get files only:

 Get-ChildItem <Folder> -File -Recurse
like image 193
Shay Levy Avatar answered Oct 30 '22 12:10

Shay Levy


Filter for files before counting:

(Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count
like image 3
jon Z Avatar answered Oct 30 '22 13:10

jon Z