Exchange 2007: Script to ouput multiple variables to comma-separated csv

Posted on 2011-10-01
Last Modified: 2012-05-12
Hello guys,

Im having abit of trouble creating a powershell script that will post the following:

DisplayName,ItemCount,ItemSize,DatabaseName for all users on all Exchange servers in the organization, and exported to a comma-separated csv file. ItemSize preferably in KB/MB.

I will then use a phpscript to import info from the csv file and insert to a mysql db.

Here is my non-workikng code :)
$ExchangeServers = Get-ExchangeServer |where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true }
$AllServers = @()
foreach ($server in $ExchangeServers)
  $collect = Get-MailboxStatistics -Server $server | ft MailboxGuid
  foreach ($objItem in $collect)

    $user = Get-MailboxStatistics -Identity $objItem | fl
    $name = $user.DisplayName
    $size = $user.TotalItemSize
    $count = $user.ItemCount
    $db = $user.DatabaseName

    $output = New-Object PSObject
    $output | Add-Member NoteProperty -Name "Name" -Value $name
    $output | Add-Member NoteProperty -Name "Size" -Value $size
    $output | Add-Member NoteProperty -Name "Count" -Value $count
    $output | Add-Member NoteProperty -Name "Database" -Value $db
    $AllServers += $output

$AllServers | export-csv c:\test\output.csv

Open in new window

Any ideas?
Question by:jjugland
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

chrismerritt earned 500 total points
ID: 36899096
Try this, Mailbox stats is not great for a full mailbox list as some mailboxes won't have any values yet (i.e. not logged onto). This script uses Get-Mailbox for the user list, and then gets the mailbox stats of each user:

$ExchangeServers = Get-ExchangeServer | where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true}

$MasterArray = @()

foreach ($Server in $ExchangeServers)
	$Mailboxes = Get-Mailbox -Server $Server.Name -ResultSize "Unlimited"
	foreach ($Mailbox in $Mailboxes)
		Write-Host -ForegroundColor "yellow" "Processing Mailbox - $($Mailbox.PrimarySMTPAddress)"
		$TempArray = @()
		$TempArray = "" | Select PrimarySMTP, DisplayName, DatabaseName, TotalItemSizeMB, ItemCount
		$MbxStats = Get-MailboxStatistics -Identity $Mailbox
		#If mailbox has MailboxStatistics then add the data
		if ($MbxStats -ne $Null)
			[string]$TempArray.PrimarySMTP = $Mailbox.PrimarySMTPAddress
			[string]$TempArray.DisplayName = $Mailbox.DisplayName
			[string]$TempArray.DatabaseName = $Mailbox.Database
			[int]$TempArray.TotalItemSizeMB = $MbxStats.TotalItemSize.Value.ToMB()
			[int]$TempArray.ItemCount = $MbxStats.ItemCount
			$MasterArray += $TempArray
		#Otherwise the TotalItemSize/ItemCount values are Null
			[string]$TempArray.PrimarySMTP = $Mailbox.PrimarySMTPAddress
			[string]$TempArray.DisplayName = $Mailbox.DisplayName
			[string]$TempArray.DatabaseName = $Mailbox.Database
			[int]$TempArray.TotalItemSizeMB = $Null
			[int]$TempArray.ItemCount = $Null
			$MasterArray += $TempArray
		Remove-Variable -Name "MbxStats" -ErrorAction "SilentlyContinue"

$MasterArray | Export-CSV "C:\Some Folder\Somefile.csv" -NoType

Open in new window


Author Comment

ID: 36899630
Perfect! Just what i needed, many thanks for your assistance :-)

Author Closing Comment

ID: 36899634
Just what i needed! Thanks alot :-)

Author Comment

ID: 36899637
Woops, double post :X

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
Check out this step-by-step guide for using the newly updated Experts Exchange mobile app—released on May 30.
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video discusses moving either the default database or any database to a new volume.

690 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