Suddenly woke up the other night and got the idea that I should create a script that counts files and folders in my PowerShell repo, and it ended up with the following script.
The script
This script counts files and folders recursively from a path specified. Let’s say you have some files and folders within the folder, then the script will count all folders, subfolders and and files within these folders.C:\temp
Then main functionality of the script happens in the process block, where the script loops through each item within the specified path using the cmdlet . Then it checks whether it is a directory and of a file contains a specified file ending. Since I was curios about how many PowerShell script files (.ps1) I had in my repo. Furthermore, If none of the Get-ChildItem check conditions is met it goes to the ifelse block and it is a file.
I also played around with a progress bar using the cmdlet. When the script runs it loads in the pace of the amount of files and folders within the specified path. This is cool, I must say. The rest of the script is the formatting of the result. Next up running the script.Write-Progress
function Measure-FilesAndFolders {
[CmdletBinding()]
param (
# File path
[Parameter(mandatory = $false)]
[String]
$Path = "C:\Users\$env:USERNAME\source\repos\PowerShell_Scripts",
# File ending
[Parameter(Mandatory = $false)]
[String]
$FileEnding = ".ps1"
)
begin {
$FileCount = 0
$DirectoryCount = 0
$FileEndingCount = 0
$i = 0
$Events = Get-ChildItem -Path $Path -Recurse
}
process {
foreach ($Item in Get-ChildItem -Path $Path -Recurse) {
$i++
if ($Item.mode.substring(0, 1) -eq "d") {
$DirectoryCount++
}
if ($Item | Where-Object { $_ -match "$FileEnding+?" }) {
$FileEndingCount++
}
else {
$FileCount++
}
Write-Progress -Activity "Counting items..." -Status "Progress" -PercentComplete ($i / ($Events).Count * 100);
}
$TotalFileCount = $FileCount + $FileEndingCount
Write-Host -ForegroundColor Cyan "`nMeasured files and folders in the path: $Path `n"
Write-Host -ForegroundColor Green "Total directory count: $DirectoryCount"
Write-Host -ForegroundColor Green "Total file count: $TotalFileCount"
Write-Host -ForegroundColor Green "Total files ending with $($FileEnding): $FileEndingCount of $TotalFileCount files`n"
}
}
Running the script
To run the above script you can use dot source, as seen in the code block below, and then run it as a regular cmdlet. I added the default path to my repo in the parameter section, so if you want it to work you must add a path that exists on your computer. When the script runs you should see the progress bar and the output of the result.
# Loading the script into memory PS C:\> . .\Measure-FilesAndFolders.ps1 # Executing the cmdlet by using the c:\temp folder instead of the default which is my PowerShell repo. PS C:\> Measure-FilesAndFolders -Path C:\temp
The result of the script
When the script runs, you should see the progress bar, as seen below.

When the script is complete you should see a similar result, most likely with different values 😉

That’s about it. Have a great evening!




