Need help with writing data to file + formatting RRS feed

  • Question

  • I have created a script which the purpose is to grab everyone's mailbox in the environment and list their name, mailbox size, item count, last person to log on, last log on time and when it was created.  I'm going to use this to review mailboxes that have been in our environment for a while but have very little content.

    The below script works.  I'm using "smith" as a test so that it pulls everyone who has smith in their display name.  

    The problem I have is it out puts the data with the table headers every single time.  I'd like it if possible to just list the headers once and then output everyone's data below that.  

    Secondly I need to pipe it out to a file (csv) so I can work with the data.  I've tried >, out-file and add-content but it either exports just one persons data or giberish.

    $mailbox = get-mailbox -identity "*smith*"
    foreach ($person in $mailbox){
    $whencreated = $person.whencreated
    Get-mailboxstatistics -Identity "$person" | ft displayname, totalitemsize, itemcount,lastloggedonuseraccount, lastlogontime,  @{Label="WhenCreated";Expression={$whencreated} } 

    Friday, June 13, 2014 2:18 PM


  • Hi,

    This'll do what you're after:

    $out = @()
    $mbxs = Get-Mailbox -Identity '*smith*'
    foreach ($mbx in $mbxs) {
        $whenCreated = $mbx.WhenCreated
        $out += Get-MailboxStatistics -Identity $mbx.SamAccountName | 
            Select DisplayName,TotalItemSize,ItemCount,LastLoggedOnUserAccount,LastLogonTime, @{n="WhenCreated";e={$whencreated}} 
    $out | Sort DisplayName | Export-Csv .\mbxStats.csv -NoTypeInformation

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by David_G_A Friday, June 13, 2014 2:38 PM
    Friday, June 13, 2014 2:29 PM

All replies