ZEM
ZEM

Reputation: 21

Passing multiple variables to Export-CSV in Powershell

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.

Upvotes: 0

Views: 6086

Answers (1)

Tim Ferrill
Tim Ferrill

Reputation: 1684

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}}

Upvotes: 1

Related Questions