How to Count Files in a Folder Using PowerShell?

You might encounter situations where you need to count the number of files present in a folder using PowerShell. In this tutorial, I will explain how to count files in a folder using PowerShell.

To count files in a folder using PowerShell, you can use the Get-ChildItem cmdlet combined with the Measure-Object cmdlet. For example, to count all files in a specific folder, you would use $fileCount = (Get-ChildItem -Path “C:\Your\Target\Directory” -File).Count. This command will return the number of files in the specified directory, excluding subdirectories and non-file items.

Count Files in a Folder Using PowerShell

One of the easiest ways to count files in a folder using PowerShell is to use the Count function. Here is a complete PowerShell example.

$folderPath = "C:\MyFolder"
$fileCount = (Get-ChildItem -Path $folderPath).Count
Write-Host "There are $fileCount files in $folderPath"

This script sets the target directory path to a variable and then uses the Get-ChildItem cmdlet to retrieve the items in the directory. The .Count method is then used to count the items. The result is displayed using Write-Host.

You can see the screenshot below after I executed the script using VS code.

Count Files in a Folder Using PowerShell

Counting Files Recursively

If you need to count all files within a folder and its subfolders (recursively) using PowerShell, you can add the -Recurse parameter.

$folderPath = "C:\MyFolder"
$fileCount = (Get-ChildItem -Path $folderPath -Recurse | Measure-Object).Count
Write-Host "There are $fileCount files in $folderPath and its subfolders"

Here, Get-ChildItem is piped to Measure-Object, which counts all objects passed to it, providing the total file count, including subdirectories.

You can see the output in the screenshot below, and it shows me exactly how many files there are in the folder and subfolders.

how to Count Files in a Folder Using PowerShell

Count Specific File Types in a folder

To count the number of a specific file type, you can use the -Filter parameter. For example, to count .txt files in a folder using PowerShell, you can write the script like below:

$folderPath = "C:\MyFolder"
$fileType = "*.txt"
$fileCount = (Get-ChildItem -Path $folderPath -Filter $fileType -Recurse | Measure-Object).Count
Write-Host "There are $fileCount text files in $folderPath and its subfolders"

This script will count all .txt files in the specified folder and its subfolders.

Exclude Folders in the Count

You might just want to count files inside a folder and you want to exclude the subfolders. You can do this by using the -Exclude parameter in PowerShell.

$folderPath = "C:\MyFolder"
$excludeFolders = @("Subfolder1", "Subfolder2")
$fileCount = (Get-ChildItem -Path $folderPath -Recurse -Exclude $excludeFolders | Measure-Object).Count
Write-Host "There are $fileCount files in $folderPath excluding certain subfolders"

This script will count all files in the directory and its subdirectories, excluding “Subfolder1” and “Subfolder2”.

Find Files larger than X MB in the Folder

For more complex filtering, you can use a ScriptBlock to count files based on certain conditions. For example, to count files that are larger than 1MB using PowerShell:

$folderPath = "C:\MyFolder"
$fileCount = (Get-ChildItem -Path $folderPath -Recurse | Where-Object { $_.Length -gt 1MB } | Measure-Object).Count
Write-Host "There are $fileCount files larger than 1MB in $folderPath and its subfolders"

The Where-Object cmdlet is used here to filter files based on size before counting them with Measure-Object.

Performance Considerations

When working with large directories, performance can be an issue. To improve performance, you can use the -File switch with Get-ChildItem to only retrieve files (skipping directories), which can speed up the command:

$folderPath = "C:\MyFolder"
$fileCount = (Get-ChildItem -Path $folderPath -Recurse -File | Measure-Object).Count
Write-Host "There are $fileCount files in $folderPath and its subfolders"

PowerShell count files in folder and subfolders

To count files in both a folder and its subfolders using PowerShell, you’ll want to use the Get-ChildItem cmdlet with the -Recurse parameter, which instructs PowerShell to include all subdirectories in its search. The -File parameter can be used to ensure that only files are counted, excluding directories from the count.

Here’s a detailed explanation of how to perform this task:

  1. Open PowerShell: You can do this by searching for PowerShell in the Start menu or by right-clicking the Start button and selecting “Windows PowerShell” from the context menu.
  2. Use Get-ChildItem: The Get-ChildItem cmdlet retrieves the files and folders in the specified path. When used with the -Recurse parameter, it will recursively search through all the subdirectories.
  3. Filter for Files: The -File parameter ensures that only files are considered, excluding directories from the results.
  4. Count the Results: Pipe the output of Get-ChildItem to the Measure-Object cmdlet, which will count the objects that have been passed to it.

Here’s the complete PowerShell script to count files in a folder and its subfolders:

$folderPath = "C:\MyFolder"
$fileCount = (Get-ChildItem -Path $folderPath -Recurse -File | Measure-Object).Count
Write-Host "There are $fileCount files in $folderPath and its subfolders"

In this script:

  • $folderPath stores the path to the target directory.
  • Get-ChildItem -Path $folderPath -Recurse -File retrieves all files in the target directory and its subdirectories.
  • Measure-Object is piped (|) the list of files and uses its .Count property to count them.
  • Write-Host is used to output the result to the console.

This command is efficient and straightforward for counting files across a directory tree. Remember to replace "C:\MyFolder" with the actual path to the directory you want to analyze.

Conclusion

PowerShell provides different cmdlets for counting files in a folder, such as recursive counts, filtering by file type, excluding directories, and more.

In this PowerShell tutorial, I have explained how to count files in a folder in PowerShell using various methods.

You may also like: