Exchange Mailbox/AD details from Powershell

Hi,

Thanks for looking (sorry if this posted in the wrong forum!).

I need to get some information from AD/Exchange, what I need to pipe the get-aduser command to get-mailboxstatistics

The information I need is:

SamAccountName, DisplayName, , lastlogintime, EmployeeID, Office, CanonicalName, MailboxSize.

Been trying to get the following working then export to CSV

$users = Get-adUser TestAccount -properties *
$users | Foreach-Object{

    $user = $_
    $stats = Get-MailboxStatistics $user

    New-Object -TypeName PSObject -Property @{
        FirstName = $user.FirstName
        LastName = $user.LastName
        SamAccount = $user.SamAccountName
        Department = $user.Department
        EmployeeID = $user.employeeID
        EmployeeNumber = $user.employeenumber
        Division = $user.Divison
        Office = $user.Office
        OU = $user.canonicalname
        Email = $user.mail
        Mobile = $user.mobilePhone
        LastLogon = $user.lastlogondate
        MailboxSize = $stats.TotalItemSize
        ItemCount =  $stats.ItemCount
            }
}

Any help would be great! Thank you




  • Edited by SaintsDT Saturday, April 25, 2015 6:38 PM
April 25th, 2015 5:55pm

$users = Get-ADUser TestAccount -Properties *
$users | Foreach-Object {

    $user = $_
    $stats = Get-MailboxStatistics $user.SamAccountName

    New-Object -TypeName PSObject -Property @{
        FirstName = $user.GivenName
        LastName = $user.SurName
        SamAccount = $user.SamAccountName
        Department = $user.Department
        EmployeeID = $user.EmployeeID
        EmployeeNumber = $user.EmployeeNumber
        Division = $user.Divison
        Office = $user.Office
        OU = $user.CanonicalName
        Email = $user.mail
        Mobile = $user.MobilePhone
        LastLogon = $user.LastLogonDate
        MailboxSize = $stats.TotalItemSize
        ItemCount =  $stats.ItemCount
    }
}

Free Windows Admin Tool Kit Click here and download it now
April 26th, 2015 7:53pm

Hi Saints,

As Helland post, you need to add ".SamAccountName" or ".name" as below:

$stats = Get-MailboxStatistics $user.SamAccountName

OR

$stats = Get-MailboxStatistics $user.Name

I have test in my lab and it runs successfully, as below:

Best regards,

April 28th, 2015 2:05am

I also changed

FirstName = $user.FirstName
LastName = $user.LastName
to
FirstName = $user.GivenName
LastName = $user.SurName
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 5:14am

Thanks both of you! cant believe I was just missing $user.SamAccountName! (spent hours over weekend on this!).

One final question, how would I export this to CSV? Tried | export-csv c:\test.csv at end but I get an error message

Thanks again!!!

April 28th, 2015 6:35am

Try this:

$summary = @()
$users = Get-ADUser TestAccount -Properties *
$users | Foreach-Object {

    $user = $_
    $stats = Get-MailboxStatistics $user.SamAccountName

    $obj = New-Object -TypeName PSObject -Property @{
        FirstName = $user.GivenName
        LastName = $user.SurName
        SamAccount = $user.SamAccountName
        Department = $user.Department
        EmployeeID = $user.EmployeeID
        EmployeeNumber = $user.EmployeeNumber
        Division = $user.Divison
        Office = $user.Office
        OU = $user.CanonicalName
        Email = $user.mail
        Mobile = $user.MobilePhone
        LastLogon = $user.LastLogonDate
        MailboxSize = $stats.TotalItemSize
        ItemCount =  $stats.ItemCount
    }
    $summary += $obj
}
$summary | Export-Csv c:\test.csv -NoTypeInformation


Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 8:01am

Thanks again for all your help!
May 1st, 2015 3:39am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics