Microsoft Exchange, Powershell, AD, MOM...

14 Kasım 2007 Çarşamba

Calculating the Total Size of Files with Same Extension under A Folder with PowerShell

As it is always noted PowerShell make a lot of things easier as it makes it easy to deal with large amount of files. Consider a scenario where one wants to see the number .exe files under a specific path including child folders. And also he may ask for the total size of these files!! The answer to this kind of questions comes in no time when you use PowerShell;) Here is how..

Now we will look for exe files under c:\temp

PS C:\ps> dir /temp *.exe


Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 23.09.2007 14:51 7718480 auth finger.exe
-a--- 23.09.2007 15:35 10959456 hda modem.exe
-a--- 23.09.2007 14:45 617392 intel sata.exe
-a--- 23.09.2007 14:51 7086272 intel wireless.exe
-a--- 23.09.2007 15:23 56896928 ms uaa.exe
-a--- 23.09.2007 14:31 837962 sd card.exe
-a--- 23.09.2007 14:50 6732048 soundmax.exe
-a--- 23.09.2007 14:47 7085952 texas media card.exe
-a--- 23.09.2007 15:03 24316376 touchpad.exe
-a--- 23.09.2007 14:40 17221472 video.exe
-a--- 23.09.2007 23:25 37783736 windvd.exe
-a--- 24.09.2007 20:02 337859 XNINSTAL.EXE


Quite easy one!! Here we use a more powershell-looked code the get the same result:

PS C:\ps> gci /temp -Recurse | where {$_.extension -eq ".exe"}


Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 23.09.2007 14:51 7718480 auth finger.exe
-a--- 23.09.2007 15:35 10959456 hda modem.exe
-a--- 23.09.2007 14:45 617392 intel sata.exe
-a--- 23.09.2007 14:51 7086272 intel wireless.exe
-a--- 23.09.2007 15:23 56896928 ms uaa.exe
-a--- 23.09.2007 14:31 837962 sd card.exe
-a--- 23.09.2007 14:50 6732048 soundmax.exe
-a--- 23.09.2007 14:47 7085952 texas media card.exe
-a--- 23.09.2007 15:03 24316376 touchpad.exe
-a--- 23.09.2007 14:40 17221472 video.exe
-a--- 23.09.2007 23:25 37783736 windvd.exe
-a--- 24.09.2007 20:02 337859 XNINSTAL.EXE

Here how we can count them:

PS C:\ps> (gci /temp -Recurse | where {$_.extension -eq ".exe"}).count
12

Lastly the code to calculate the size of files with ".exe" extension

PS C:\ps> gci /temp -Recurse | where {$_.extension -eq ".exe"} | %{$total+=$_.Length}
PS C:\ps> $total
177593933
PS C:\ps> $total/1024/1024
169,366772651672

The last one is to give the result as MB as it is more easy to read!!



And after this instruction here is something more useful. This script will look for all files under the given path including the child folders and gives you a result grouped by extensions. Each extension group lists the number of files and total size of that files with same extension. Lastly it shows the total size and number of all files under the given path and also shows how much time passed since the beginning of the script..



#written by Can Dedeoglu 4 AVEA @ 02.10.2007
#known bug: calculates files with same extension but with different capitals twice!!
#known bug2: if an extension appears only once under the search path, script counts this as none but calculates file size just fine
$time1=[datetime]::now
$ext=gci $args -recurse | where {$_.PSIsContainer -eq $false} | foreach {$_.extension} | sort -Unique
for($i=0;$i -lt $ext.count;$i++)
{
$total=0
gci $args -recurse | where {$_.PSIsContainer -eq $false -and $_.extension -eq $ext[$i]} | foreach {$total+=$_.length}
$count=(gci $args -recurse | where {$_.PSIsContainer -eq $false -and $_.extension -eq $ext[$i]}).count
$countall+=$count
$sizeall+=$total
write-host $ext[$i] -foreground magenta
write-host Total Size: $($total/1024/1024)MB
write-host Total Count: $($count)
write-host -------------------------
}
write-host Total number of all files: $countall
write-host Total size of all files: $($sizeall/1024/1024)MB
$time2=[datetime]::now
write-host Elapsed Time: $($time2-$time1)


Here is the output:



PS C:\ps> .\Get-FileUsage.ps1 /temp
.exe
Total Size: 169,366772651672 MB
Total Count: 12
-- -- -- -- -- -- -- -- -- -- -- -
.msi
Total Size: 12,52001953125 MB
Total Count:
-- -- -- -- -- -- -- -- -- -- -- -
.TXT
Total Size: 0,00594711303710938 MB
Total Count: 2
-- -- -- -- -- -- -- -- -- -- -- -
.vbs
Total Size: 0,0575618743896484 MB
Total Count:
-- -- -- -- -- -- -- -- -- -- -- -
Total number of all files: 14
Total size of all files: 181,950301170349 MB
Elapsed Time: 00:00:00.1716913



Enjoy!!

Hiç yorum yok: