Last active
January 4, 2019 08:20
-
-
Save techthoughts2/65de31916346e96f19bc to your computer and use it in GitHub Desktop.
Contains a simple example for retrieving total directory size for all files within a directory. Also contains a more in depth example for retrieving file size as well as other related file information from a directory path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#how to get files and ignore directories | |
$sizes = Get-ChildItem -Path C:\dell\suu -Recurse -ErrorAction Stop | Where-Object { ! $_.PSIsContainer } | Select-Object -ExpandProperty Length | |
#simple example | |
#--------------------------------------------------------- | |
$path = "C:\Test" | |
#--------------------------------------------------------- | |
$size = (Get-ChildItem -Path $path -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum | |
"Folder Size: $size Bytes" | |
'Folder Size: {0:n2} MB' -f ($size/1MB) | |
#more in depth example | |
#--------------------------------------------------------- | |
$path = "C:\test" | |
[double]$intSize = 0 | |
[double]$totalNumFiles = 0 | |
#--------------------------------------------------------- | |
try{ | |
if(Test-Path $path){ | |
$files = Get-ChildItem -Path $path -Recurse -Force ` | |
-ErrorAction SilentlyContinue | |
} | |
else{ | |
Write-Output "The path you specified is not valid" | |
return | |
} | |
#get information about files (combined size, total #) | |
$i = 0 | |
foreach ($objFile in $files){ | |
$i++ | |
$intSize = $intSize + $objFile.Length | |
Write-Progress -activity "Adding File Sizes" -status "Percent added: " ` | |
-PercentComplete (($i / $files.length) * 100) | |
$totalNumFiles++ | |
} | |
#round for nice numbers | |
$intSize = [math]::round($intSize / 1GB, 0) | |
#generate output | |
Write-Output "----------------------------------------------" | |
Write-Output "Scan results for: $path" | |
Write-Output "----------------------------------------------" | |
Write-Output "Total # of file found: $totalNumFiles" | |
Write-Output "----------------------------------------------" | |
Write-Output "Total size of all files: $intSize GB." | |
Write-Output "----------------------------------------------" | |
Write-Output "Top 10 Largest Files found:" | |
$files | select Directory,Name,` | |
@{Label=”Length”;Expression={[math]::round($_.Length/1GB, 2)}} | ` | |
sort Length -Descending| select -First 10 | ft -AutoSize | |
Write-Output "----------------------------------------------" | |
} | |
catch{ | |
Write-Error $_ | |
} | |
#get file encoding information | |
$global:gitbin = 'C:\Program Files\Git\usr\bin' | |
Set-Alias file.exe $gitbin\file.exe | |
file.exe * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment