I’ve looked everywhere I can to find an answer to this, but come up empty, so maybe someone can put me on track.

I want to export a CSV of all Exchange mailboxes that includes details of the OU the user account is in. I seem to be able to get it to list everything except the OU.

Can anyone enlighten me please?

4 Spice ups
Import-Module ActiveDirectory 
Get-ADUser -filter * | select SamAccountName,DistinguishedName | export-csv c:\wherever\DN.csv

Thanks Duke - is there any way to get this information out of Exchange at the same time I do a mailbox stats export?

It used to be so easy from Exchange to just export the window content. Whilst powershell is obviously much more powerful, doing simple things with it seems so much more difficult!

What kind of information do you want from Exchange? Mailbox database, server, etc.?


```
#Run from Exchange Server with RSAT or workstation with RSAT and implicit remoting to Exchange.

#Next line not needed for PowerShell 3.0+
Import-Module ActiveDirectory

$Users = Get-ADUser -Filter * -Properties DistinguishedName,Title,LastLogonDate,Enabled,LegacyExchangeDN | Where-Object LegacyExchangeDN

$Info = @()

ForEach ($User in $Users) {
       $OU = ([adsi]"LDAP://$($user.DistinguishedName)").parent
       $MailboxInfo = Get-Mailbox -Identity $user.SamAccountName 

       $Item = New-Object -TypeName PSObject -Property @{
            Displayname = $MailboxInfo.DisplayName;
            Account = $User.SamAccountName
            Title = $User.Title
            AccountEnabled = $User.Enabled
            LastLogonDate = $User.LastLogonDate
            OU = ($OU -split "LDAP://")[1];
            Database = $MailboxInfo.Database;
            ServerName = $MailboxInfo.ServerName
    }
    $Info += $Item    
}
$Info | Out-GridView
```

Duke’s example brings only AD account information - this one should merge the mailbox and AD info only if the account has a LegacyExchangeDN value.

1 Spice up

Oh I see! I did think I might be missing the point a bit, lol.

Perfect - thank you very much Rob.

1 Spice up

Any time! You might need to tweak that a bit to get the information you want, just let us know and we can help you.