0

Hey guys I'm having a Powershell 2.0 problem that is driving me crazy. My objective: Create a script that determines the size of the Documents folder along with the current user on the same row, but in two different fields in a csv file. I have tried the following scripts so far:

$startFolder= "C:\Users\$env:username\Documents"
$docinfo = Get-ChildItem $startFolder -recurse | Measure-Object -property length -sum |
$docinfo | Export-Csv -Path C:\MyDocSize\docsize.csv -Encoding ascii -NoTypeInformation

This script works and exports the folder size (sum), along with some columns that I don't need, "Average, Maximum, Minimum, and a property column that has "Length" as a value. Does anyone know how to just show the sum column and none of the other stuff? My main question is however, how do I pass "$env:username" into "$docinfo" and then get "$docinfo" to pass that into a CSV as an additional column and an additional value in the same row as the measurement value?

I tried this:

$startFolder= "C:\Users\$env:username\Documents"
$docinfo = Get-ChildItem $startFolder -recurse | Select-Object $env:username
$docinfo | Export-Csv -Path C:\MyDocSize\docsize.csv -Encoding ascii - NoTypeInformation

This will pass just the current username to the csv file, but without a column name, and then I can't figure out how to incorporate the measurement value with this. Also I'm not even sure why this will pass the username because if I take the "Get-ChildItem $startFolder -recurse" out it will stop working.

I've also tried this script:

$startFolder= "C:\Users\$env:username\Documents"
$docinfo = Get-ChildItem $startFolder -recurse | Measure-Object -property length -sum  
New-Object -TypeName PSCustomObject -Property @{
UserName = $env:username
DocFolderSize = $docinfo    
} | Export-Csv -Path C:\MyDocSize\docsize.csv -Encoding ascii -NoTypeInformation

This script will pass the username nicely with a column name of "UserName", however in the "DocFolderSize" column instead of the measurement values I get this string: Microsoft.PowerShell.Commands.GenericMeasureInfo

Not sure what to do now or how to get around this, I would be really appreciative of any help! Thanks for reading.

1 Answer 1

1

Give this a try"

Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum | Select Sum, @{Label="username";Expression={$env:username}}

The @{Label="username";Expression={$env:username}} will let you set a custom column header and value.

You can customize the Sum column using the same technique:

Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum | Select @{Label="FolderSize";Expression={$_.Sum}}, @{Label="username";Expression={$env:username}}

And if you want to show the folder size in MB:

Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum | Select @{Label="FolderSize";Expression={$_.Sum / 1MB}}, @{Label="username";Expression={$env:username}}
Sign up to request clarification or add additional context in comments.

14 Comments

Thank you! This works, however if I just select sum it exports to csv as "@{Sum=10915869}" how can I just get the number? Thank you so much for you help.
never mind it only shows up like that when I try it with my object, "$startFolder= "C:\Users\$env:username\Documents" Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum | Select Sum, @{Label="username";Expression={$env:username}} | Export-Csv -Path C:\MyDocSize\info.csv\MyDocSize\info.csv -Encoding ascii -NoTypeInformation" This works perfectly and is much simpler than what I had. Thank you so, so much!
One more question though, how do I customize the Sum column name? "Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum | @{Label="MyDocSize";Expression={Select Sum}}, @{Label="username";Expression={$env:username}} | Export-Csv -Path C:\MyDocSize\info.csv -Encoding ascii -NoTypeInformation" Throws a "Expressions are only allowed as the first element of a pipeline" error
Added to the answer. That should meet your needs.
If you are still looking at this, is there a way to change sum from bytes to MBs within that expression? You've already helped so much I hate to ask for more, but thanks if you can!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.