How to export csv

Hharis 21 Reputation points
2022-01-15T05:31:36.503+00:00

Hi Guys,

I don't know how to export this powershell script. how to export to csv files ?

$Users = Import-Csv c:\Sources\Query\alias.csv
foreach ($id in $users)
{
Get-MailboxStatistics $id.alias | Sort-Object DisplayName, Database, TotalItemSize –Descending | ft @{label=”User”;expression={$.DisplayName}},@{label=”Database”;expression={$.Databasename}},@{label=”Total Size (MB)”;expression={$_.TotalItemSize.Value.ToMB()}}
}

Thank You,

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-01-15T09:14:34.6+00:00

    Hi @Hharis ,

    you can try it this way (not tested by myself):

    $Users = Import-Csv c:\Sources\Query\alias.csv  
    $mailboxStats = @()  
    foreach ($id in $users) {  
        $userStats = Get-MailboxStatistics $id.alias | Sort-Object DisplayName, Database, TotalItemSize –Descending |  
            Select-Object @{label = ”User”; expression = { $_.DisplayName } }, @{label = ”Database”; expression = { $_.Databasename } }, @{label = ”Total Size (MB)”; expression = { $_.TotalItemSize.Value.ToMB() } }  
        $mailboxStats = $mailboxStats + $userStats   
    }  
    $mailboxStats | Export-Csv -Path "c:\temp\mailboxstats.csv" -NoTypeInformation  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Dominik Meier 66 Reputation points
    2022-01-15T09:20:55.81+00:00

    Hey @Hharis

    Did you try to add an output at the end of the query?

    Example:
    foreach ($item in $ITGlueTest.data)
    {
    $item.attributes | export-csv C:\organization.csv -Append
    }

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-01-15T15:40:31.603+00:00

    Here's another way to do this without the need for intermediate array manipulation:

    Import-Csv c:\Sources\Query\alias.csv |
        ForEach-Object {
            Get-MailboxStatistics $_.alias
        } |
            Sort-Object DisplayName, Database, TotalItemSize -Descending | 
                Select-Object  @{n = "User"; e = { $_.DisplayName } }, 
                                @{n = "Database"; e = { $_.Databasename } }, 
                                @{n = "Total Size (MB)"; e = { $_.TotalItemSize.Value.ToMB() } } |
                    Export-Csv c:\junk\MailBoxStats.csv -NoTypeInformation
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.