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
  • 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 70

Expert Comment

by:Chris Dent
ID: 35115934

Why not use Export-Csv?

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


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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 70

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 70

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

How to Backup Ubuntu to Amazon S3

CloudBerry Backup offers automatic cloud backup and restoration for Linux. It has both GUI and command line interface (CLI) ensuring its flexibility in use. Find out more

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…
how to add IIS SMTP to handle application/Scanner relays into office 365.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now