PowerShell - Summary Of Files By Extension

Knowing in advance all the possible file types is a more efficient way to produce this type of report, but letting the code figure that out is more practical.  This report will start at the given $Target directory and summarize the number of files and total bytes by extension, in alphabetical order, to the whatever $ReportFile you select.

# Note: This report was written and tested using PowerShell V3
# Change the starting directory and report name to suit your needs
$Target = "C:\YourChoiceOfDirectory"
$ReportFile = "C:\YourChoiceOfReportName.txt"
$AllTypes = @()
# Write Report Header Lines
$Message = "All File Summary Report"
($Message) | Out-File $ReportFile
$Message = "                       "
($Message) | Out-File $ReportFile -Append
$Message = "FileType        Files                    Bytes"
($Message) | Out-File $ReportFile -Append
$Message = "---------  ----------  -----------------------"
($Message) | Out-File $ReportFile -Append
# Get every filename and produce an array of unique extensions

$List = Get-ChildItem $Target -File -Recurse
Foreach ($Item in $List) {
   $FileName= $Item.FullName
   $FileSplit = $FileName.split(“.”)
   $FilePieces = $FileSplit.Count
   $FileType = $FileSplit[$FilePieces-1]
   $AllTypes += $FileType
$extensions = $AllTypes | Sort-Object -Unique
# Get a list of files for a specific extension, count the number of
# files and their total size and write that info to the report file.

# Repeat for each extension found.
Foreach ($ext in $extensions)  {
   $Filename = Get-ChildItem $Target -File *.$ext -Recurse
   $Files = 0; $Size = 0;
   Foreach ($Item in $Filename) {
     $Size = $Size + $Item.Length
   $ext = $ext.PadRight(10,' ')
   $Files = $Files.ToString('N0').PadLeft(11,' ')
   $Size = $Size.ToString('N0').PadLeft(25,' ')
   $Message = "$ext$Files$Size"
   ($Message) | Out-File $ReportFile -Append

No comments:

Post a Comment

Upload CSV Files to Google BigQuery

This Google App Script gets each file in the "ToBeUploaded" folder in Google Drive and creates a Google BigQuery load job with tha...