none
quick script output question RRS feed

  • Question

  • I have a PS Script to generate mailbox statistics.  Right now, the script uses command Write-Output $obj at the end for output which displaying on the screen.  I need an output as csv file.  Without modifying the output command is there an easy way that I can make the output into a csv file?  

    Thanks,

    Sunday, May 12, 2019 8:38 PM

Answers

  • Hi,

    You can add the export command at the last of your script:

    Export-csv C:\output.csv -NoTypeInformation

    For example, we used the following command to export some mailbox statistics:

    Get-Mailbox -RecipientTypeDetails usermailbox | select DisplayName,PrimarySmtpAddress,@{label="TotalItemSize";expression={(Get-MailboxStatistics $_.Name).TotalItemSize}} | Sort-Object DisplayName | export-csv C:\Users\administrator.CONTOSO\Desktop\mailboxsize.csv -NoTypeInformation

    Here are the test results:

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 13, 2019 6:59 AM
    Moderator

All replies

  • Hi,

    You can add the export command at the last of your script:

    Export-csv C:\output.csv -NoTypeInformation

    For example, we used the following command to export some mailbox statistics:

    Get-Mailbox -RecipientTypeDetails usermailbox | select DisplayName,PrimarySmtpAddress,@{label="TotalItemSize";expression={(Get-MailboxStatistics $_.Name).TotalItemSize}} | Sort-Object DisplayName | export-csv C:\Users\administrator.CONTOSO\Desktop\mailboxsize.csv -NoTypeInformation

    Here are the test results:

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 13, 2019 6:59 AM
    Moderator
  • Hi Lydia,

    Thanks for looking into my question.  In my case the Write-Output command is in the script.  I do need the command in the script so I can view the output first.  To redirect the output to a csv file, instead of changing the command I use the following command:

    .\Get-MailboxStats.ps1 | Export-Csv C:\temp\MailboxStats.csv -NoTypeInformation

    It worked fine.

    Your command is also good.  I have saved a copy.


    Thanks.

    Friday, May 17, 2019 4:32 AM