• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

Help with adding Office attribute to Powershell script

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
1 Solution
use below mentioned Active Directory custom search Ldap query for getting list of user having office location set as London;

naifyboy123Author Commented:
OK thanks - but how do i encorporate that within my powershell script?
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 $( $_.properties.distinguishedname ) | 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.
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

use Exchange powershell and excute this command;

Get-User | ft Displayname, office  | export-csv c:\<file-name>.csv
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

naifyboy123Author Commented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now