Error handling in a Powershell script

Posted on 2012-08-23
Last Modified: 2012-08-28
Greetings Powershell Experts,

I have written a small Powershell script (with the help of various Experts here on Experts-Exchange) that basically allows me to input a department in our organization and generate a list of the users in that department along with their mailbox size.  The code for the script is as follows:

$Department = Read-Host "What department do you want the mailbox size report for?"
$Users = Get-user -Filter {Department -like $Department} | Select-Object SamAccountName
$results = foreach ($user in $Users) {
Get-MailboxStatistics $User.samaccountname | Select-object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}}
$Results | Format-Table -AutoSize

This is providing the information that I want in format that I want.  The only thing wrong is if a user has never logged into their mailbox, they are not included in the output.  In the powershell console I see a warning that states:

WARNING: There is no data to return for the specified mailbox 'routie', because it has not been logged on to.

Is there a way to handle this warning in my script?  

As always, thanks in advance.

Question by:ndalmolin_13
    LVL 16

    Expert Comment

    by:Rajitha Chimmani
    If you are using Powershell 2.0 you can add -WarningAction SilentlyContinue to Get-MailboxStatistics command.
    LVL 52

    Expert Comment

    by:Manpreet SIngh Khatra
    Not sure but if something like this will help

    $warningpreference = "SilentlyContinue"

    Now we look a litte bit further. The variable $warningpreference defines what happens if a script uses the write-warning cmdlet in order to make some output. If you set this variable to the value of silentlycontinue the output is suppressed. The default of this variable is Continue whitch means that warningmessages are displayed in yellow color on the console. If you want to get more details on preference variables you can read the PowerShell help

    - Rancy
    LVL 6

    Accepted Solution

    Give this a whirl. Any users that haven't logged on before will be written to a file in C:\Temp called UsersNotLoggedOnYet.txt. This should supress the warning message using the -WarningAction parameter.

    $Department = Read-Host "What department do you want the mailbox size report for?"
    $Users = Get-user -Filter {Department -like $Department} | Select-Object SamAccountName
    $results = @()
    foreach ($user in $Users) {
        $mailboxstats = Get-MailboxStatistics $User.samaccountname -WarningAction SilentlyContinue | Select-object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}} 
        if ($mailboxstats) {
            $results += $mailboxstats
        } else {
            Add-Content "C:\Temp\UsersNotLoggedOnYet.txt" "$user"
    $Results | Format-Table -AutoSize

    Open in new window


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now