Adding users to a blank array and getting the mailbox size of each user in that array using Powershell

Posted on 2012-08-22
Last Modified: 2012-08-23
Hello Powershell Experts,
Powershell is one of those things that I don’t get to do all that often at work, so I just don’t have a great handle on the basics.  What I’m trying to accomplish today is:
1.      Get a list of users in a department
2.      For each user in that list, I want to determine the mailbox size on our Exchange server

For the first goal, I can use the following command to get the list of users:
      Get-qaduser –department sales | Select-object logonname

The command above produces a list that looks like the following:

I would like the list to look like:

I think I need to use an array to do this.  My question is how do I add each logonname to a blank array?  I know I set the blank array as:
      $users = @()

To get the mailbox size for a single user, I would do the following:
get-mailboxstatistics emusta | Select-Object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}

How would I run through the array of users generated in step one to get the mailbox size for each one?  Would it be something as simple as:

get-mailboxstatistics $Users | Select-Object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}

I’m sorry for asking such lame questions, but I’ve tried everything I can think of and any help would be appreciated.
Question by:ndalmolin_13
    1 Comment
    LVL 6

    Accepted Solution

    I've not had chance to test this, but it should work. If not, let me know and I'll be able to have a proper look tomorrow.

    $users = Get-qaduser –department sales | Select-object -expand logonname
    $users | Foreach-Object {
        get-mailboxstatistics $_ | 
        Select-Object @{l="User";e={$_.DisplayName}},@{l="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article is intended to be a 2 part Primer to get someone up and running quickly and efficiently in Powershell, but also including small useful tips that you might not find elsewhere.   One of the things I see often is people starting out usi…
    Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 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

    23 Experts available now in Live!

    Get 1:1 Help Now