Skip to main content

Cloud

Useful Exchange PowerShell One-Liners

After working on various Exchange-related projects you start to accumulate some useful scripts and tools. For me, PowerShell has proven to be an invaluable tool and although I usually can’t find enough time to develop ellaborate scripts, I do occassionally come across some common but useful one-liners in some situations.
Here’s a few of these that you might find useful:
Show mailbox statistics (excluding system mailboxes)
Get-MailboxStatistics -server mbxserver1 | where {$_.ObjectClass -NotMatch ‘(SystemAttendantMailbox|ExOleDbSystemMailbox)’}|sort-object TotalItemSize -Descending | format-table DisplayName, ItemCount,@{expression={$_.TotalItemSize.Value.ToMB()};label="TotalItemSize(MB)"}, Database -autosize
List your permissions on all mailbox databases
Get-MailboxDatabase|Get-ADPermission -User jdoe
Remove your permissions on all mailbox databases (you inadvertently added receive-as for the wrong account to all databases)
Get-MailboxDatabase|Remove-ADPermission -User jdoe -ExtendedRights Receive-As
Bulk creation of mailbox accounts
import-csv import.csv | foreach {$pswd = ConvertTo-SecureString $_.Password -asplaintext -force; new-mailbox -alias $_.alias -name $_.name -userprincipalname $_.upn -database $_.database -org employees -Password $pswd -displayname $_.name -firstname $_.firstname -lastname $_.lastname -samaccountname $_.samaccountname}
The import.csv looks like this:
firstname,lastname,initials,alias,password,database,userprincipalname,displayname,samaccountname,name
John,Smith,S,JSmith,P@ssw0rd,MBX1Mailbox Database,JSmith@contoso.com,John Smith,JSmith,John Smith
Report on mailbox quotas
get-mailbox -resultsize unlimited | select displayname,alias,issuewarningquota,prohibitsendquota,prohibitsendreceivequota |sort displayname | ft -autosize
Determine which mail users have mismatched "mail" attributes and primary SMTP address (the OAB generator won’t create these accounts in the address book unless these are the same)
Get-Mailbox -resultsize unlimited -ignoredefaultscope:$true | select-object displayname,alias,recipienttype,windowsemailaddress,primarysmtpaddress,externalemailaddress | where {$_.windowsemailaddress -notmatch $_.primarysmtpaddress}| Export-CSV c:mailrpt.csv -NoClobber:$false -NoTypeInformation
Override database limits and set user’s mailbox limits
Set-Mailbox jsmith -UseDatabaseQuotaDefaults:$false -IssueWarningQuota:250MB -ProhibitSendQuota:300MB -ProhibitSendReceiveQuota:unlimited
Configure a custom email address policy
Set-EmailAddressPolicy -Identity "Default Policy" -EnabledEmailAddressTemplates smtp:%g.%s@contoso.com,smtp:%m@contoso.com,SMTP:%g.%i.%s@contoso.com
Get mailbox folder size (in MB) for a particular user
get-mailboxfolderstatistics jsmith | ft FolderPath, ItemsInFolder, @{Label="FolderSize(MB)";expression={$_.FolderSize.ToMB()} } -auto
You can find and piece together your own one-liners by examining the issue or task you may be facing with Exchange and start with the TechNet site for a particular command you have in mind. The "power" really comes when you start stringing several of these commands together. I know I will keep adding to my personal collection of useful one-liners and hopefully I can also find the time to start creating more complex PS scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.