Get-MailboxReport.ps1 – PowerShell Script to Generate Mailbox Reports

One of the Exchange Server administration tasks I perform almost every day is creating mailbox size reports. There are a few different reasons that I create these reports, such as planning a mailbox migration project, responding to a storage capacity alert for a particular database, or providing a specific team of people with a report of their mailbox sizes.

Now it is pretty easy to get the sizes for Exchange mailboxes and to handle the formatting of the Exchange 2010 mailbox statistics so that they are easier to perform calculations on, but it gets a bit boring running those commands day after day.

Even worse, after running the commands to create a CSV report I still had to open that in Excel, remove the unwanted details, use Excel formulas to convert the values from bytes to megabytes, sort them into order, and so on. Again not difficult, just boring after doing it hundreds of times.

So I created a PowerShell script, Get-MailboxReport.ps1, to do all of the heavy lifting for me, and I’m sharing that script with you here.

Download the script file here: Get-MailboxReport.ps1 (downloaded 735 times so far)

Let’s take a look at how the script works. Here if a video to demonstrate:

I’ve included help information within the script itself so you can use Get-Help to discover how to run the script.

[PS] C:\Scripts\demo>Get-Help .\Get-MailboxReport.ps1

NAME
    C:\Scripts\demo\Get-MailboxReport.ps1

SYNOPSIS
    Get-MailboxReport.ps1 - Mailbox report generation script.

SYNTAX
    C:\Scripts\demo\Get-MailboxReport.ps1 [-database ] []

    C:\Scripts\demo\Get-MailboxReport.ps1 [-file ] []

    C:\Scripts\demo\Get-MailboxReport.ps1 [-server ] []

    C:\Scripts\demo\Get-MailboxReport.ps1 [-mailbox ] []

    C:\Scripts\demo\Get-MailboxReport.ps1 [-all] []

DESCRIPTION
    Generates a report of useful information for
    the specified server, database, mailbox or list of mailboxes.
    Use only one parameter at a time depending on the scope of
    your mailbox report.

RELATED LINKS
    For more script details, a video demo, or to give feedback please go to:

http://exchangeserverpro.com/powershell-script-create-mailbox-size-report-exchange-server-2010

REMARKS
    To see the examples, type: "get-help C:\Scripts\demo\Get-MailboxReport.ps1 -examples".
    For more information, type: "get-help C:\Scripts\demo\Get-MailboxReport.ps1 -detailed".
    For technical information, type: "get-help C:\Scripts\demo\Get-MailboxReport.ps1 -full".

Depending on which parameter you use the output will vary.

  • If you use the -mailbox parameter to query a single mailbox, then the output will appear in the console window. I don’t really see the need to output a single mailbox’s details to a CSV file.
  • If you use any of the other parameters, -server, -database, -file, or -all, the output will be written to a CSV file in the same folder you’re running the script from.

Once you’ve generated the CSV report you can open it with Excel and begin to analyze the data.

Download the script file here: Get-MailboxReport.ps1 (downloaded 735 times so far)

Change Log:

  • 6/02/2012 – V1.0 – Initial version
About Paul Cunningham

Paul is a Microsoft Exchange Server specialist for one of Australia's largest companies, and is the Publisher of ExchangeServerPro.com. He is also an MCP, MCSA, MCSE, MCTS, and an MCITP for Exchange Server 2007/2010. Connect with Paul on Twitter, LinkedIn and Google+.

Comments

  1. Devang Patel says:

    This is Awsome. I tried it and works smoothly. Before using this scripte i used to get the results manually by just entering the commands every time.

    I have one query about the text file that we need to create for getting the results for particular users. which fileds should i enter in the text file.

    Thanks
    Devang

  2. Sorin says:

    Hi Paul,

    Can u make this script work with server parameter? So it can list all the mailboxes from the server at once.

    Thanks,
    Sorin.

  3. Script has had a major overhaul and re-release, so much so that I’m just going to call this new one V1.0 :-)

  4. Edward Walton says:

    question. is it possilbe to script a way to find all emails with attachments over 25meg.

    thanks

  5. Chris says:

    Hi Paul,

    That script very useful, very well documented as well. I’ll try to improve it adding if the user has mailbox default storage limits.

    Thanks for sharing!

  6. Rinku says:

    hi

    good work paul, thanks for sharing,

  7. kevin says:

    Paul

    I have an issue where the powershel is preventing my from runnin gthe script. ” the file is not digitally signed” is the complaint from the PS!!

  8. Josh says:

    Keep getting an error ….
    “Missing opening “(” after keyword ‘for’.
    At C:\Scripts\get-mailboxreport.ps1:48 char:5
    + For m <<<< ore script details, a video demo, or to give feedback please go to:

    Am I missing something?
    Exch 2007

    • Hi Josh, it is written for PowerShell v2.0. Try running:

      $host

      …to see which version you’ve got. If you’re on version1.0 you can try and edit the script to remove all that help info at the start, and see if that works, but I haven’t specifically tested it on v1.0 to check for any other issues.

  9. Kerry says:

    Excellent script! Thanks for the work and the updates. I know that this will be used much by myself and the consultants I work with.

    Thanks again

  10. Frank says:

    Getting this error message.

    [PS] C:\temp\Exchange\PowerShell\other>.\Get-MailboxReport.ps1 -database MBX1VP\MbxDb-YNHH-002

    Security Warning
    Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your
    computer. Do you want to run C:\temp\Exchange\PowerShell\other\Get-MailboxReport.ps1?
    [D] Do not run [R] Run once [S] Suspend [?] Help (default is “D”): R
    Cannot process argument transformation on parameter ‘Database’. Cannot convert value “MBX1VP\MbxDb-YNHH-002″ to type “M
    icrosoft.Exchange.Configuration.Tasks.DatabaseIdParameter”. Error: “‘MBX1VP\MbxDb-YNHH-002′ is not a valid value for th
    e identity.
    Parameter name: Identity”
    + CategoryInfo : InvalidData: (:) [Get-Mailbox], ParameterBindin…mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Mailbox

  11. Frank says:

    Never mind. Got it now. Still getting this message:

    Security Warning
    Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your
    computer. Do you want to run C:\temp\Exchange\PowerShell\other\Get-MailboxReport.ps1?
    [D] Do not run [R] Run once [S] Suspend [?] Help (default is “D”): R

    I ran Set-ExecutionPolicy Unrestricted

  12. Jeff Strubberg says:

    Paul;

    Great script! I removed a few of the output fields (we only have one mail server and one mail database, so those fields weren’t meaningful for us). At the moment I am trying to sort the resulting csv file via the script to put the mailboxes in order largest to smallest. Could use some help getting this to work.

    Once I’ve got that figured out, this script is going to save me a ton of time. I’ve got it running as a schedlued task on my exchange server and emailing me the resulting csv on the first of every month.

    • In the last Else block try adding a Sort-Object to the $report | Export-CSV line like this:

      $report | Sort-Object “Size (Mb)” -Desc | Export-Csv -Path $reportfile -NoTypeInformation

  13. starchaser says:

    I am trying to run this script using the -database or -file parameter and it’s generating a blank csv file. The csv file generated is of 0 bytes.

    • That will happen if no mailbox info could be retrieved.

      For -database make sure you’re supplying a value that would work if you ran “Get-Mailbox -database “.

      For -file try removing the -ignoredefaultscope switch from the script line

      “if($file) { $mailboxes = @(Get-Content $file | Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }”

  14. starchaser says:

    Well, I tested the -database parameter works with .\GetMailboxReport.ps1 -database ‘ServerName\StorageGroupName\DataabaseName’

    However, .\GetMailboxReport.ps1 -file does not work. I tried Display Name, Primary SMTP address, UPN but it doesn’t work. It creates a blank 9 byte csv file.

    • Ah yep, see comment above. Try removing -ignoredefaultscope from the line:

      “if($file) { $mailboxes = @(Get-Content $file | Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }”

      I’ll add it to my bug list.

  15. starchaser says:

    Fantastic! It worked. Great script.

    Thanks!

  16. David Taig says:

    Hi Paul,

    Great report for Migrations. As I need the Alias to perform the migration, I have added this in the first column:

    $userObj | Add-Member NoteProperty -Name “Alias” -Value $mb.Alias

    I also added sort “Size (Mb)” -Desc

    I did run this on a combined DC and Exchange 2007 SP3 Server – Enabled and Expires is blank. OK on other Exchange 2007 SP3 Server, so seems issue having DC and Exchange 2007 together..

  17. turbomcp says:

    Thanks again paul for an amazing time saver(and also teaches me powershell)
    i only had one issue when i ran it through 2010 ps(dont know if it matters) using -file
    i got cannot move something about distinguage name cannot be used or must be used:) when using -ignorescope
    so i removed the ignorescope for the file part of the script and it ran like a charmer:)
    Thanks again
    Much appreciated

    • Yep, little bug I need to work on there. If you have a multi-domain forest you’d just need to put your management shell session into viewentireforest $true mode to see all the mailboxes in the report.

      Or if you’re a single domain forest it doesn’t matter, script will work as you’ve modified it :-)

  18. Kbart2000 says:

    Hello Paul,

    Thanks again för sharing your script. Works great, I just have a question… How can I do to find the right character in the csv file .. I m having in my exchange swedish names with those: å,ä,ö.

    Is it a parameter to add ?

    • Sorry, I don’t understand the question.

      • Kbart2000 says:

        When Im having a look at the csv file after I ran the script,
        If for example the name of the user is Åsa Bergström, the result in the csv file is ?sa Bergstr?m

        Its not the right character, I would like to get on my csv file the swedish characters like : the å the ä and the Ö instead of “?”

        Hope I m more clear now? :)

      • Ah, I understand now. Not sure of the solution though, I’ll see if I can find something.

  19. Christa Wilson says:

    Nice report. I’d like to put it into a scheduled task for a weekly/monthly report that’s emailed to me. I’m wondering how to call upon the output file as it will change each time it’s ran.

    • Hi Christa, probably two ways you could do that.

      1) Remove the file name randomization. So instead of

      $reportfile = “MailboxReport-$timestamp-$random.csv”

      have simply

      $reportfile = “MailboxReport.csv”

      2) The file name is stored as $reportfile, so if you’re adding email functionality to the script you can always reference the file name by that variable, no matter what the real name is.

  20. Tom G says:

    I guess I am a retard because I can’t get it to run. I keep getting the following error:

    [PS] C:\Windows\system32>c:\scripts\get-Mailboxreport.ps1
    The term ‘c:\scripts\get-Mailboxreport.ps1′ is not recognized as the name of a cmdlet, function, script file…..

    What am I doing wrong? I am not an expert with PowerShell but this shouldn’t be this hard. thanks

    • Hi Tom,

      Navigate to the folder where you’ve saved the script file, then run it with:

      .\Get-Mailboxreport.ps1

      You’ll need to supply one of the parameters for which type of report you want to run. You can see the help info by running:

      Get-Help .\Get-MailboxReport.ps1

      Or watch the video above for a demonstration.

      Hope that helps.

  21. Tom G says:

    I had already tried it that way but I ran it again. See below. I get the same error. It doesn’t seem to matter how I run it. Can you help further? This is driving me nuts. I will watch the video as well.

    [PS] C:\scripts>.\Get-MailboxReport.ps1
    The term ‘.\Get-MailboxReport.ps1′ is not recognized as the name of a cmdlet, function, script file,…….

    thanks

Speak Your Mind

*