I was recently asked what tool would be best to report the number of items in, and the size of, every folder in a particular file share. As an IT Architect I have numerous tools at my disposal that would be able to acquire the data my business partner needed. A few lines of PowerShell was the easiest to implement.
If you’ve used PowerShell for long you already known that Get-ChildItem is the cmdlet to retrieve things under a parent. Files, Folders, Items, you can list them all with GCI. Open PowerShell and type GCI then press enter, depending on your PowerShell profile settings, you should see a list of all your user profile sub folders. This cmdlet will form the basis of our report script.
Of course, the full solution is a little more complicated than that. To generate a useful report we’ll use the Get-ChildItem command to get a list of folders in our path. Then we’ll loop through each folder with the same command again to get a list of the files.
We’ll build an array that contains the count and length (size) properties of each file. Finally we’ll export that array to a csv file in your documents folder. With a little more effort you could generate an HTML report and upload it to a web page or embed it in an email. See some of my other articles for how.
# Get-FileReport.ps1 #Author: Kevin Trent, Whatdouknow.com #Right click .ps1 file and Open with PowerShell #Enter Filepath or share path. $location = Read-Host "Enter Top Level File Path" $folders = Get-ChildItem -Path $location -Recurse -Directory $array = @() foreach ($folder in $folders) { $foldername = $folder.FullName # Find files in sub-folders $files = Get-ChildItem $foldername -Attributes !Directory # Calculate size in MB for files $size = $Null $files | ForEach-Object -Process { $size += $_.Length } $sizeinmb = [math]::Round(($size / 1mb), 1) # Add pscustomobjects to array $array += [pscustomobject]@{ Folder = $foldername Count = $files.count 'Size(MB)' = $sizeinmb } } # Generate Report Results in your Documents Folder $array|Export-Csv -Path $env:USERPROFILE\documents\file_report.csv -NoTypeInformation
Hi,
Nice script but i have an error :
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name
must be less than 248 characters.
LikeLike
That isn’t an error caused by the script. Somewhere on the system you ran it on is a file(s) that is violating the Max_Path system variable. One of the easiest ways to deal with it is to move the files into a shorter folder structure. There are also several utility apps out there that can help you.
LikeLike
Yes it’s a windows limitation…
But i have seen scripts that avoid the error by using the robotcopy command in their script…
LikeLike
In that case Vincent, use those scripts 😎
LikeLike
Hi Kevin,
Great post and a great script that has helped me, however some sub-folders (in fact a lot) in the directories I run this against are empty.
Is there a way to not write empty (0 files) folders to the CSV log file?
I guess on the flipside to help other people, is also the opposite and to only report empty folders.
Thanks Darren
LikeLike
My apologies, I just noticed that WordPress did not publish my reply to you. The answer is yes but not by modifying the export itself. Export-CSV assumes that you have everything the way you want it before you invoke the command. Swap out the section below and the new If statement will filter out 0 MB items except when they are the parent folder of something populated.
# Add pscustomobjects to array
If ($sizeinmb -ne 0) {
$array += [pscustomobject]@{
Folder = $foldername
Count = $files.count
‘Size(MB)’ = $sizeinmb}
}
}
LikeLike
Hi Kevin,
Great script. Is it possible to update a script to provide creation date and modified date?
LikeLike
Wow, a great script, and so cleverly presented that even a noob can make sense of it; great potential for learning (should one be teachable).
I am puzzled by the output: my result is a single line for each of the top-level folders in the target directory (appropriate file and file size numbers) but although the script calls for -Recurse, no child folders are reported. What am I missing?
Thanks in advance for any hints and suggestions!
LikeLike
Nice teaching presentation, and a bit more robust than most folder, file count scripts.
I have a problem with the output failing to include subfolders’ files. I’ve read of ‘-Recurse’ wrecking the output when collecting both directory and file information (and being a total noob, am hesitant to experiment with your code!) Was this expected to return values for folders and subfolders (some of the folders contain only folders—no my design!), or have I expected some magic the script isn’t designed to provide?
LikeLike
The folder paths should be listed in the report even if they are empty. Can you post some screenshots of the issue you are seeing?
LikeLike
Hi Kevin
Is it possible to update a script to provide Modified Date, Date Created, Item type folder, security permissions at the folder level
LikeLike
HI Kevin,
Great script. Is it possible to update a script to provide below fields
Modified Date , Date Created, Item type folder, security permissions at the folder level (including nested folders)
LikeLike