Help with adding Office attribute to Powershell script

Posted on 2011-05-09
Last Modified: 2012-06-21
I have the following powershell script but would like to only show users where their Active Directory 'Office' attribute is London so i can only report on users from that location:- all help gratefully received. thanks

Get-MailboxStatistics | where {$_.ObjectClass –eq “Mailbox”} | Sort-Object TotalItemSize –Descending | ft

@{label=”User”;expression={$_.DisplayName}},@{label=”Total Size


Limit”;expression={$_.StorageLimitStatus}} -auto
Question by:naifyboy123
    LVL 4

    Expert Comment

    use below mentioned Active Directory custom search Ldap query for getting list of user having office location set as London;


    Author Comment

    OK thanks - but how do i encorporate that within my powershell script?
    LVL 16

    Expert Comment

    To elaborate you can use the DirectorySearcher as one method to find the users and then pipe that into the Get-Mailbox command.
    $searcher = New-Object DirectoryServices.DirectorySearcher
    $searcher.pagesize = 100
    $searcher.Filter = "(&(objectCategory=user)(objectClass=user)(physicalDeliveryOfficeName=London))"
    $searcher.PropertiesToLoad.AddRange( @( "physicalDeliveryOfficeName","distinguishedname") )
    $searcher.findall() | Foreach-Object {
       Get-Mailbox -identity $( $ ) | the rest of you script here

    Open in new window

    Or if you're happy to use something like the Quest AD Cmdlets you can use the Get-QADUser command.
    Get-QADUser -Office "London" | Foreach-Object {
       Get-Mailbox -identity $( $_.dn ) | the rest of you script here

    Open in new window

    Or if your environment supports it you can use the Get-ADUser Cmdlets instead.
    LVL 4

    Expert Comment

    use Exchange powershell and excute this command;

    Get-User | ft Displayname, office  | export-csv c:\<file-name>.csv
    LVL 13

    Accepted Solution

    Try this:
    Get-Mailbox -Filter {office -eq "London"} | Get-MailboxStatistics |?{$_.objectclass -eq "Mailbox"} | Sort-Object TotalItemSize –Descending | ft @{label=”User”;expression={$_.DisplayName}},
        @{label=”Total Size (MB)”;expression={$_.TotalItemSize.Value.ToMB()}},
        @{label=”Items”;expression={$_.ItemCount}},@{label=”Storage Limit”;expression={$_.StorageLimitStatus}} -auto

    Open in new window


    Author Closing Comment

    I tried all suggestion but could only get this one to work. Maybe I was doing something wrong with the others - not sure. thanks guys for all your help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
    This video discusses moving either the default database or any database to a new volume.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now