Hello,

First off my powershell capability is very low so I am struggling with this. If anyone can help I would greatly appreciate it.

I am trying to run a small commnd / script to get the following in a CSV for sorting:

Displayname

PrimarySMTPAddress

Department

TotalitemSize

I have tried a few things I have fround on teh net but cant seem to either get it to output what I want or it breaks altogether. This is the best I could come up with but it isn’t outputting everything I need:

Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,PrimarySmtpAddress,TotalItemSize, @{Name=“Department”;expression={(get-user $_.alias).department}}

Any help is appreciated.

7 Spice ups

To get the mailbox size details, you’ll need to use the Get-MailboxStatistics cmdlet as well.

Where are you getting the user information from?

try this

$outtbl = @()
$users = Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’}
$users | % {
  $x = Get-MailboxStatistics $_ |select TotalItemSize
  $t = New-Object PSObject -Property @{
    Name = $_.Name
    TotalItemSize = $x.TotalItemSize
    Department=$_.Department
    Email=$_.WindowsEmailAddress
  }
  $outtbl += $t
}
$outtbl | Export-Csv c:\ussersxx.csv -NoTypeInformation

## To get only enabled acounts

Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’ -and $_.useraccountcontrol -notlike '*accountdisabled*'}
2 Spice ups

Here’s another approach. I created a data table object and cast the 4 criteria you requested into each column and corresponding row. If the AD “EmailAddress” property doesn’t suffice for primary SMTP address, you could just call the “get-mailbox” cmdlet in place of pulling that data from the get-aduser cmdlet.

$newTable=New-Object System.Data.DataTable "Info"

$col0=New-Object System.Data.DataColumn DisplayName,([string])
$col1=New-Object System.Data.DataColumn PrimarySMTPAddress,([string])
$col2=New-Object System.Data.DataColumn Department,([string])
$col3=New-Object System.Data.DataColumn TotalItemSize,([string])

$newTable.Columns.AddRange(@($col0,$col1,$col2,$col3))

$users=Get-AdUser -Properties Department,EmailAddress,DisplayName -Filter * -SearchBase "ou=parentcontainer,dc=domain,dc=com"

foreach ($user in $users)
{
    $row0=$newTable.NewRow()
    $row0.DisplayName=$user.DisplayName
    $row0.PrimarySMTPAddress=$user.EmailAddress
    $row0.Department=$user.Department
    $row0.TotalItemSize=(Get-MailboxStatistics -id $user.samaccountname).TotalItemSize.value
    $newTable.Rows.Add($row0)
}

$newTable | sort DisplayName | ft -AutoSize
1 Spice up

Thank you I will try these out after my meetings

Both answers worked like champs and thank you both for writing that for me. I wish I could choose more than one best answers in this case, but it would only let me chose one.