In this post we are going to look at a list of useful Exchange PowerShell commands that should be apart of any Sysadmin’s arsenal when managing an Exchange environment.
Notes on regular expressions/wildcards:
Three of the codes that I’ve found to be particularly useful and you will see being used in quite a few examples below – ‘^‘ (Begins With), ‘$‘ (Ends With) and ‘|‘ (OR) and of course (*) is a wildcard for all.
The beginning of the command dictates the result:
- Get – Gathers existing setting
- Add – Adds a new non-existing change, cannot change pre-existing
- Set – Amends current setting
To fit your requirements you can change the beginning of any of the commands below
Remotely use PowerShell commands on Exchange Server
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServer/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
Remotely use PowerShell commands on Office 365 Online Exchange Server
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Authentication Basic -Credential $UserCredential
Import-PSSession $Session
Behind a proxy server?
If you’re behind a proxy server, run this command first –
$ProxyOptions = New-PSSessionOption -ProxyAccessType , where the ProxyAccessType value is IEConfig, WinHttpConfig, or AutoDetect
$UserCredential = Get-Credential
$ProxyOptions = New-PSSessionOption -ProxyAccessType IEConfig$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Authentication Basic -Credential $UserCredential -SessionOption $ProxyOptions
Import-PSSession $Session
Mailbox/Exchange information
Get all Exchange Servers:
Get-ExchangeServer | fl
Get all mailbox databases:
Get-MailboxDatabase | fl
Get internal and external url for the ECP web page:
Get-ECPVirtualDirectory | Format-List Name,InternalURL,ExternalURL
Get a specific mailbox on a specific Exchange server, including status information:
Get-MailboxDatabase -Identity “MailboxDatabase” -Server “Server” -Status | Format-List
Get Mailbox details:
Get-Mailbox -identity “MailboxName” | fl
Search for multiple mailboxes with similar or matching Display Name:
Get-Mailbox | Where-Object {$_.displayname -like ‘*User*Name*‘} | fl
Get-Mailbox -resultsize unlimited | Where-object {$_.displayname -match “^UserMailboxName“} | fl
Mailbox statistics:
Get-MailboxStatistics -Filter ‘displayName -eq “NameOfMailbox“‘ | fl
Mailbox statistics from a specific database:
Get-MailboxStatistics -Database “Recovery” -Filter ‘displayName -eq “NameOfMailbox“‘ | fl
Mailbox Access permissions
Get mailbox permissions:
Get-MailboxPermission -identity “MailboxName” | fl
Set mailbox permission:
Set-MailboxPermission -identity “MailboxName” -user “UserRequiringAccess” -AccessRights FullAccess
Search for mailboxes by name and then add mailbox permission:
Get-Mailbox -resultsize unlimited | Where-object { $_.displayname -match “^UserMailboxName“} | ForEach-Object {Set-MailboxPermission -identity $_.displayname -user “UserRequiringAccess” -AccessRights FullAccess}
Mailbox Calendars
Find calendar:
Get-Mailbox | Get-MailboxFolderStatistics | Where-Object {$_.folderpath -like ‘/calendar/*Users*Calendar*‘ } | select identity, name | ft -wrap
Add calendar permissions:
Add-MailboxFolderPermission -Identity “MailboxName:\Calendar” -User UserRequiringAccess -AccessRights Reviewer
Search for mailboxes by name and get calendar permissions:
Get-mailbox -resultsize unlimited | Where-object {$_.displayname -match “^UserMailboxName“} | ForEach-Object {Get-MailboxFolderPermission “$($_.displayname):\Calendar”} | ft LegacyExchangeDN,Foldername,user,accessrights -Wrap -AutoSize
Search for mailboxes by name and add calender permissions to all eligible calendars:
Get-mailbox -resultsize unlimited | Where-object {$_.displayname -match “^UserMailboxName“} | ForEach-Object {Add-MailboxFolderPermission “$($_.displayname):\Calendar” -User “UserRequiringAccess” -AccessRights “Editor” }
Give one user access to all mailboxes calendars:
Get-mailbox | foreach-object {add-MailboxFolderPermission $_”:\Calendar” -User youruser@domain.com -AccessRights Reviewer}
Remove calendar permissions that match a name:
Get-mailbox | Where-object {$_.name -match “^svc_|^admin|^Test|^User“} | ForEach-Object {Remove-MailboxFolderPermission “$($_.name):\Calendar” -user “UserWithAccess“} | fl Displayname, User, AccessRights
Add permissions to Imbedded Calendar in a Calendar:
Add-MailboxFolderPermission “Mailbox:\Calendar\Conference lines & Internal Meeting Rooms\” -user “UserRequiringAccess” -Accessrights PublishingEditor
Search-Mailbox
Search Mailbox for calendar meetings with a specific subject then delete object
Search-Mailbox -identity “UsersMailbox” -SearchQuery kind:meetings AND subject:”Tea Party at 5!” -DeleteContent
Search Mailbox for reoccurring calendar meetings with a specific subject in a set date range then delete object
Search-Mailbox -identity “UsersMailbox” -searchquery kind:meetings AND Subject:”Daily Meeting” (Recurring) AND Received:”2018/01/01 10:00:00..2018/02/01 13:00:00“
Delete calendar meetings in a Mailbox using PowerShell – Exchange
How to / Fix – Delete recurring meetings using PowerShell – Exchange Mailbox
Mailbox Send Rights and Forwards
Set multiple users to send on behalf rights:
Set-Mailbox “MailboxName” -GrantSendOnBehalfTo @{add=”User1“,”User2“}
Send-As permissions:
Add-ADPermission “MailboxDisplayNameinAD” -ExtendedRights Send-As -user “UsersFirst.Lastname“
Set message to appear in shared mailboxes sent items:
Set-Mailbox -identity “MailboxName” -MessageCopyForSendOnBehalfEnabled $true -MessageCopyForSentAsEnabled $true
Sets email forwarding on mailbox:
Set-Mailbox -Identity “MailboxName” -ForwardingAddress “xyz@gmail.com” -DeliverToMailboxAndForward $true
Disconnected Mailboxes (Deleted Mailboxes)
Connects mailbox via Mailbox ID to AD User:
Connect-mailbox –database “DatabaseName” –Identity 1a6683df-8de2-4123-ba81-1144dad7deea –User “UsersUsername“
See all disconnected mailboxes:
Get-MailboxStatistics -Server ServerName | where { $_.DisconnectDate -ne $null } | select DisplayName,DisconnectDate,Database,Identity
Search for individual disconnected mailboxes:
Get-MailboxStatistics -Server ServerName | where { ($_.DisconnectDate -ne $null) -and ($_.DisplayName -like “Sarah*“)} | select DisplayName,DisconnectDate,Database,Identity
Check that the mailbox has been re-connected successfully if doubts:
Get-MailboxStatistics -Server ServerName | where { $_.DisconnectDate -eq $null} | where DisplayName -eq “” | select DisplayName,DisconnectDate,Database,Identity,User
Mailbox customer attributes and address book policies
Set a custom attribute on a mailbox:
Get-Mailbox -OrganizationalUnit “OU=Test,DC=Contoso,DC=com” | Set-Mailbox -CustomAttribute1 “test“
Create an address list containing all users and distribution groups where CustomAttribute15 equals TAIL:
New-AddressList -Name “AL_TAIL_Users_DGs” -RecipientFilter {((RecipientType -eq ‘UserMailbox‘) -or (RecipientType -eq “MailUniversalDistributionGroup“) -or (RecipientType -eq “DynamicDistributionGroup“)) -and (CustomAttribute15 -eq “TAIL“)}
Thanks for reading – feel free to follow and stay updated 🙂 View sysadminguides’s profile on Facebook View GuidesSysadmin’s profile on Twitter View 115372466162675927272’s profile on Google+
Pingback: Restoring a Mailbox or specific Mailbox data in Exchange | Windows SysAdmin Hub
Pingback: How to / Fix – Delete recurring meetings using PowerShell – Exchange Mailbox | Windows SysAdmin Hub
Pingback: Delete calendar meetings in a Mailbox using PowerShell – Exchange | Windows SysAdmin Hub