Saturday, August 1, 2015

Exchange 2013 Cmdlets

I get asked often for various cmdlets on how to perform actions in Exchange PowerShell. So I have decided to post them on the blog. I will be updating this every time I find myself using a new one often so be sure to bookmark this page.


Export Mailbox folder sizes:
Get-Mailbox -Identity User | Get-MailboxFolderStatistics | Select Identity,FolderPath,FolderSize,ItemsInFolder | Sort-Object ItemsInFolder | fl | out-File C:\Temp\User-Mbox.txt

Export UPN of all users with an archive:
Get-Mailbox -Archive | Select-Object UserPrincipalName | Export-Csv -Path C:\Temp\AssignLicenseOptions.csv

Retrieve Message Tracking logs, sort by TotalBytes and save to txt file:
 Get-MessageTrackingLog -EventId SEND -Start "9/1/2011 06:00:00" -End "9/1/2011 20:00:00" -resultsize unlimited | Sort-Object TotalBytes -Descending | FT Sender, MessageSubject, TotalBytes > C:\test.txt

 Retrieve send-as permissions on a mailbox that is not the account tied to the mailbox:
 Get-Mailbox -Identity "ltemple" | Get-ADPermission | where { ($_.ExtendedRights -like "*Send-As*") -and -not ($_.User -like "NT AUTHORITY\SELF") } | select Identity, User, ExtendedRights, IsInherited | FT -Wrap

 Formatted List with Mailbox Size in Megabytes (MB) to CSV File:
Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName,{$_.TotalItemSize.Value.ToMB()} | export-csv -path C:\<FILENAME>.csv

Alternatively, you can specify the MBD also:
Get-MailboxDatabase -Identity "MailboxDatabase" | Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName,{$_.TotalItemSize.Value.ToMB()} | export-csv -path C:\Temp\MailboxSize.csv

Get Archive Size:
Get-Mailbox | Get-MailboxStatistics -Archive | Select-Object DisplayName,{$_.TotalItemSize.Value.ToMB()},Database | export-csv -path C:\Filename.csv

Formatted List with Mailbox Size in Megabytes (MB) Output to Screen:
Get-Mailbox | Get-MailboxStatistics | Format-Table DisplayName,{$_.TotalItemSize.Value.ToMB()} -auto

Get whitespace in DB's:
2010/2013:
Get-MailboxDatabase -Status | Select Servername, Name, AvailableNewMailboxSpace
2007:
Get-MailboxServer | Get-MailboxDatabase | Select Server, StorageGroupName, Name, @{Name="Size";expression={"{0:N2}" -f ((get-mailboxstatistics -database $_.Identity | Measure-Object -Property TotalItemSize,TotalDeletedItemSize -Sum |Select-Object Sum |Measure-Object -Property Sum -Sum).Sum.ToString() /1mb)}}

Get Mailbox Move Statistics: 
Get-MoveRequest | Get-MoveRequestStatistics | ft displayname,*stat*,perc*,totalmailboxsize

Only in Progress Moves:
Get-MoveRequest –MoveStatus InProgress | Get-MoveRequestStatistics | ft displayname,*stat*,perc*,totalmailboxsize

For a constant update:
while (1 -eq 1) { Get-MoveRequest | Get-MoveRequestStatistics; Start-Sleep -Seconds 2; Clear-Host; }

Empty Dumpster for User: 
search-mailbox -identity "MBauer" -searchdumpsterOnly -DeleteContent

Dump Message Tracking to GridView:
get-messagetrackinglog -Start "7/14/2015 8:00:00 AM" -End "7/14/2015 4:30:00 PM" -ResultSize Unlimited | select timestamp, ClientIp, ClientHostname, ServerIp, ServerHostname, ConnectorId, Source, EventId, InternalMessageId, Sender, {$_.Recipients}, {$_.RecipientStatus},MessageSubject, SourceContext, MessageId, TotalBytes, RecipientCount, RelatedRecipientAddress, Reference, ReturnPath, MessageInfo | Out-GridView

Dump Message Tracking Results to CSV: 
get-messagetrackinglog -MessageSubject "Test Message to Verify Tracking Details" -Start "1/26/2014 9:00:00 PM" -End "1/26/2014 9:30:00 PM" | select timestamp, ClientIp, ClientHostname, ServerIp, ServerHostname, SourceContext, ConnectorId, Source, EventId, InternalMessageId, MessageId, {$_.Recipients}, {$_.RecipientStatus}, TotalBytes, RecipientCount, RelatedRecipientAddress, Reference, MessageSubject, Sender, ReturnPath, MessageInfo | Export-Csv "c:\Temp\Track-results.csv"

Get Message Tracking for whole domain: 
get-messagetrackinglog -Server "Exchange-server-name" -Start "7/1/2010 11:34:00 AM" -End "8/10/2010 9:44:00 AM" -resultsize unlimited |where {$_.Sender -like "*@domain.org"}
  
Get Unified Messeging Extensions: 
Get-UMMailbox |ft name,extensions,UMDialPlan,UMMailboxPolicy
  
Get List of SMTP Addresses and export to CSV:
Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}} | Export-CSV "C:\Temp\List-SMTP-Address.csv"

Get List ActiveSync Devices:
2013:
$Mailboxes = Get-Mailbox
$Mailboxes | ForEach {Get-MobileDeviceStatistics -Mailbox:$_.Identity} | Select Identity,DeviceType,DeviceUserAgent,DeviceID,LastSyncAttemptTime,LastSuccessSync | Export-CSV -Path C:\temp\ActiveSyncDevices.csv

2010:
Get-ActiveSyncDevice -ResultSize Unlimited | Select-Object UserDisplayName, Name, DeviceUserAgent | Export-Csv C:\Temp\ActiveSyncDevices.csv

Exch2007:
Get-Mailbox | ForEach {Get-ActiveSyncDeviceStatistics -Mailbox:$_.Identity} | ft DisplayName, Devicetype, DeviceUserAgent, LastSuccessSync

Get ActiveSync information for specific user:
Get-ActiveSyncDeviceStatistics -Mailbox blye | ft DeviceType, DeviceUserAgent, LastSuccessSync

Grant Rights to a user for a public folder and all sub-folders:
Get-PublicFolder –Identity “\IT” –Recurse | Add-PublicFolderClientPermission –User pponzeka –AccessRights PublishingEditor

See if anyone has sent or received email from a specific domain: 
Get-MessageTrackingLog -resultsize unlimited |where-object {$_.Recipients -like “*@renovatingla.com,*@bhkruper.com,*@usaveremodeling.com,*@rmogency.com” -AND $_.EventId -eq “Send”} |ft -auto >>C:\Temp\MailDomainSearch.txt

Batch move mailbox archives from text file:
$Mailboxes = Get-Content .\TextFile.txt
For ($Start = 0; $Start -lt $Mailboxes.length; $Start++) {New-MoveRequest –Identity `$Mailboxes[$Start] -ArchiveOnly -ArchiveTargetDatabase 'MailboxDatabaseName'}

Remove messages from queue with specific subject:
Remove-Message -Filter {Subject -eq "ENTERSUBJECT HERE"} -WithNDR $false
  
Export User's Contacts Folder:
New-MailboxExportRequest -Mailbox user1 -IncludeFolders “#Contacts#” -ExcludeDumpster -filepath \\server\share\user1.pst

Dump Mail Relay to txt file:
(Get-ReceiveConnector "exchange2007\allowedrelay").RemoteIPRanges | select Lowerbound,Upperbound,RangeFormat | sort-object Lowerbound| export-csv c:\rc.txt –NoTypeInformation

Move messages with a specific subject:
Get-Mailbox -Identity “MBauer” | Search-Mailbox -SearchQuery subject:”Alert: Host Parent Partition CPU Utilization high Resolution state: ”,from:”Honeypot@taskrepository.com” -TargetMailbox “MBauer” -TargetFolder “SCOM\CPU”

Update OAB:
Update-OfflineAddressBook “default offline address book”

Update DB Catalog Only (Re-Seed Content Index State_):
Update-MailboxDatabaseCopy "Mailboxes M-O\MailboxServer" -CatalogOnly

Set Domain on OWA/ECP Login Page:
Set-OwaVirtualDirectory ServerName\"owa (Default Web Site)" -LogonFormat Username -DefaultDomain TaskRepository

DL Auditing:
Get-DistributionGroup | Select-Object PrimarySMTPAddress | Sort-ObjectPrimarySMTPAddress | Export-CSV DL-ALL.csv -notype
Get-MessageTrackingLog -Server EXCH1 -EventId Expand -ResultSize Unlimited |Sort-Object RelatedRecipientAddress | Group-Object RelatedRecipientAddress |Sort-Object Name | Select-Object @{label="PrimarySmtpAddress";expression={$_.Name}}, Count | Export-CSV DL-Active.csv -notype
$file1 = Import-CSV -Path "DL-ALL.csv"
$file2 = Import-CSV -Path "DL-Active.csv"
Compare-Object $file1 $file2 -Property PrimarySmtpAddress -SyncWindow 500 |Sort-Object PrimarySmtpAddress | Select-Object -Property PrimarySmtpAddress |Export-Csv DL-Inactive.csv -NoType

Tracking Log Explorer:
Get-MessageTrackingLog -ResultSize Unlimited -Start "June 18 2015” | Out-GridView

Disable Calendar Repair:
Set-Mailbox -Identity username -Calendar Repair Disabled $True

Get all unhealthy Exchange resources:
$test = get-healthreport -server CATDALEXCH01 | where {$_.alertvalue -ne “healthy”}
foreach ($line in $test) {$line.entries | where {$_.alertvalue -ne “healthy”} | ft -auto}

Remove Automatic Mapping of Mailbox:
Remove-MailboxPermission -Identity celine -User ashley -AccessRights FullAccess

Add-MailboxPermission -Identity celine -User ashley -AccessRights FullAccess -AutoMapping:$false

No comments:

Post a Comment