Skip to content

Instantly share code, notes, and snippets.

@dmtrek14
Created July 15, 2022 16:24
Show Gist options
  • Save dmtrek14/6c1c4ed0029f6317129ae3f3e354fec2 to your computer and use it in GitHub Desktop.
Save dmtrek14/6c1c4ed0029f6317129ae3f3e354fec2 to your computer and use it in GitHub Desktop.
A little Powershell script to iterate through files (including those in subfolders), get file sizes, and write out all data to an Excel file.
$files = Get-ChildItem "{path to folder being analyzed}" -Recurse
## new up an excel file
$ExcelObj = New-Object -comobject Excel.Application
$ExcelWorkBook = $ExcelObj.Workbooks.Add()
$ExcelWorkSheet = $ExcelWorkBook.Worksheets.Item(1)
$ExcelWorkSheet.Name = 'File sizes'
$ExcelWorkSheet.Cells.Item(1, 1) = 'Filename'
$ExcelWorkSheet.Cells.Item(1, 2) = 'Size (MB)'
$ExcelWorkSheet.Rows.Item(1).Font.Bold = $true
$ExcelWorkSheet.Rows.Item(1).Font.size = 15
$ExcelWorkSheet.Columns.Item(1).ColumnWidth = 28
$ExcelWorkSheet.Columns.Item(2).ColumnWidth = 28
$counter = 2
## now loop through files and write to excel
foreach ($f in $files) {
$isDirectory = (Get-Item $f) -is [System.IO.DirectoryInfo]
if ($isDirectory -eq $false) {
$fsize = $f.Length / 1MB
$ExcelWorkSheet.Cells.Item(1).Rows.Item($counter) = $f.Name
$ExcelWorkSheet.Cells.Item(2).Rows.Item($counter) = $fsize
$counter ++
}
}
$ExcelWorkBook.SaveAs('C:\Users\{path to save location}')
$ExcelWorkBook.close($true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment