Link to home
Start Free TrialLog in
Avatar of bndit
bndit

asked on

Get files total size in directory structure with Powershell

Hello,
I have a challenge with getting the cumulative size for files of a particular extension (.pst) within a directory structure. Here's what I have:

$var1 = get-childitem "C:\rootdirectory" -recurse
$var2 = $var1 | where {$_.extension -eq ".pst"} | select fullaname | out-file -filepath "C:\output.txt"

The above lines allow me to crawl the rootdirectory and find all the .pst files. However, I'd like the output to give me the cumulative size for these files. I'd appreciate it if you point me in the right direction.
Thanks,
SOLUTION
Avatar of Aard Vark
Aard Vark
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

This is a barely changed alternative.

Just because knowing more than one way can sometimes be quite useful :)

Chris
(Get-ChildItem "C:\rootdirectory" -Recurse -Filter *.pst | Measure-Object -Sum Length).Sum | Out-File C:\output.txt
Write-Host "Total Size: $($intSize/1MB)"

Open in new window

Avatar of bndit
bndit

ASKER

Interesting...with the first script the output captures the fullname for each file, but the intSize variable doesn't get a value. The second script tell me the total size of the files but again, the intSize variable doesn't get a value.
Avatar of bndit

ASKER

If I do something like this...it works

$filesize = (get-childitem -filter *.txt | measure-object -sum length).sum
$filesize/1mb

However, I'd like to consolidate the two scripts into one. I'd like to see the output file display the list of files with each file's size, and the total size.

C:\test\file1.txt  - 10kb
C:\test\file2.txt - 12kb
C:\test\file3.txt - 20kb

total number of files: 3
total size: 42kb

Sorry yes, that was me being a bit too fast with the posting button.

For the amendment...

$Files = Get-ChildItem -Filter *.txt
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } > "Output.txt"
"" >> "Output.txt"
"Total Number of Files: $(([Array]$Files).Count)" >> "Output.txt"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Kb)Kb" >> "Output.txt"

It's a bit messy with all those redirections, we might execute the entire thing as a sub-expression and output at the end. Like this:

$( $Files = Get-ChildItem -Filter *.txt
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" }
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Kb)Kb" ) > "Output.txt"

Note that > and >> are equivalent of Out-File without and with the -Append (respectively). If you're curious... Get-Help about_redirection.

Chris
Avatar of bndit

ASKER

Thanks Chris...that worked great with the exception that I'm trying to round the numeric outputs to 2 decimals...so I looked up at the .NET Framework formatting methods but it didn't work...here's the modified script

$( $Files = Get-ChildItem "C:\users\ramon.alvarez" -Filter *.txt
$Files | %{ "$($_.FullName) - "{0:N2}" -f $($_.Length/1Kb)Kb" }
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: "{0:N2}" -f $(($Files | Measure-Object -Sum Length).Sum/1Kb)Kb" ) > "C:\users\ramon.alvarez\desktop\Output.txt"

I'm either using the formatting method incorrectly or there's another way to do it.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bndit

ASKER

It does...thanks for taking the extra time to explain. I ran the script and it displays the info correctly.

On a different note, I really want to get a good understanding Powershell; what resources/material do you recommend for someone like me who's learning PowerShell?

Ah... see... that question pops up a fair bit and I don't have much of an answer I'm afraid. My learning style is rather, er, lacking in structure, more of a random exploration of things that capture my attention and interest.

Some alternatives to my bad method below :)

BSonPosh (https://www.experts-exchange.com/M_4238767.html) has a number of links in his profile that may help. His Blog has a few bits as well.

Learnctx might have a better suggestion, I'm sure he'd have more points in this zone if I stopped hogging all the questions ;)

The .NET Framework reference is invaluable, especially once you're happy converting the syntax.

HuddledMasses (http://huddledmasses.org/) has some very good stuff, a bit deeper into PS than the average page.

And it's well worth keeping an eye on the MS PowerShell Newsgroup, even just watching the questions go by can help.

Naturally, we'll always be here to answer questions on anything that comes up if all else fails :)

Chris
Avatar of bndit

ASKER

Thank you. I'm sure I'll be corresponding with you again as I have many more PowerShell challenges  =)
Avatar of bndit

ASKER

thx