How to export powershell output to csv

T Crha 381 Reputation points
2023-04-25T12:12:10.5166667+00:00

Hello everyone, I have been tasked to create an automatic report that will be emailed as csv each day, showing sizes of folders on a particular server. I have googled and slightly modified following cmdlet:

Get-ChildItem "<path to folder>" | Where-Object { $.PSIsContainer } | ForEach-Object { $.FullName + " -- " + [Math]::Round((Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB, 2) + " MB" } 

But, when I pipe the result to Export-Csv (with specified path and delimiter), I get only this in the csv file:
User's image

Can you please help me to create a csv from this? Out file works fine, but this way I am not able to delimit the values. Thank you very much, Tomas

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,216 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,441 Reputation points
    2023-04-25T12:38:02.9166667+00:00

    How about this.

    Get-ChildItem "C:\temp" | Where-Object { $_.PSIsContainer } | ForEach-Object { 
        [PSCustomObject]@{
            'Name' = $_.FullName; 
            'MB' =  [Math]::Round((Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB, 2) 
        }
    } | Export-Csv -Path "c:\temp\report.csv" -NoTypeInformation
    Get-Content "c:\temp\report.csv"
    

0 additional answers

Sort by: Most helpful