Powershell Script - Formatting

Posted on 2011-03-10
Last Modified: 2012-05-11
I have a script that used to be formatted correctly (The text file had all the headers lined up properly) in Exchange 2007.  Now in Exchange 2010 the formatting is off and I cannot figure out how to get it to line up.

The Script is Below and the sample text file with the header formatting is also attached.

Any Help would be great
###Send mailbox statistics script

###Variables - change these to match what you want
$FromAddress = ''
$ToAddress ='
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = ""
$filename = "C:\test\mailboxes-$(get-date -f MMddHHssyyyy).txt"

Get-PSSnapIn -Reg | Add-PSSnapin -ea 0

###This gets the mailbox stats
Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft -autosize | 
  Select-Object DisplayName, @{name="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount | out-file $filename -width 1000

###This creates the mail message and adds the stats text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, 
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment($filename)

###This section sends the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer

Open in new window

Question by:Travis Hahn
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
  • 4
  • 3
LVL 12

Expert Comment

ID: 35106186

Try the following...change line no 14

Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName, @{name="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount -autosize | out-file $filename

Author Comment

by:Travis Hahn
ID: 35108876
Almost - but it added a "last Logon: colum as well...

See Below
LVL 71

Expert Comment

by:Chris Dent
ID: 35115934

Why not use Export-Csv?

Or even ConvertTo-Html if you want it inline in the mail.

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why


Author Comment

by:Travis Hahn
ID: 35128655
Because I dont want my inbox clogged up with HTML or CSV files, and I don't want a CSV or HTML file sitting on my email server storage.  A text file takes up far less size.  Plus it used to work just fine in Exchange 2007
LVL 71

Expert Comment

by:Chris Dent
ID: 35128782

CSV is plain text, it should take up less space than your TXT file simply because it doesn't have all the white space required to justify the columns.


Author Comment

by:Travis Hahn
ID: 35131521
okay so would the code then be | Export-CSV $filename  

Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName, @{name="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount -autosize | Export-Csv $filename

or would I have to give the exact location c:\test\ $filename?
LVL 71

Accepted Solution

Chris Dent earned 500 total points
ID: 35132301
Relative names are fine, but for this example I've used a full name as you did originally, then I'd use Send-MailMessage to send the mail rather than the longer way you have from older versions (Send-MailMessage is new with PowerShell 2).
$FromAddress = ''
$ToAddress = ''
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = ""
$Filename = "C:\test\mailboxes-$(get-date -f MMddHHssyyyy).txt"

Get-Mailbox | Get-MailboxStatistics | 
  Sort-Object TotalItemSize -Descending | 
  Select-Object DisplayName, @{name="TotalItemSize(MB)";expression={ $_.TotalItemSize.Value.ToMB() }}, ItemCount |
  Export-Csv $Filename -NoTypeInformation

Send-MailMessage -To $ToAddress -From $FromAddress -SmtpServer $SendingServer -Subject $MessageSubject `
  -Body $MessageBody -Attachments $FileName

Open in new window

The only thing I should note is the ` character on the Send-MailMessage line. That lets it carry the parameters onto the next line, it's important that it remains.



Author Comment

by:Travis Hahn
ID: 35132367
I changed line 6 to .CSV and it was all good.  And I went from 24 lines to 14 lines of code.  Thank you Chris; much appreciated.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Note: This is the second blog post in a series on email clearinghouses (   Every month t…
Learn how ViaSat reduced average response times for IT incidents from 10 minutes to 30 seconds.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to:…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

735 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