Skip to content

Instantly share code, notes, and snippets.

@scotgabriel
Last active April 4, 2021 15:35
Show Gist options
  • Save scotgabriel/2ccd98fdec917e0dcfd07d453d4d1aa0 to your computer and use it in GitHub Desktop.
Save scotgabriel/2ccd98fdec917e0dcfd07d453d4d1aa0 to your computer and use it in GitHub Desktop.
Powershell commands to interact with Microsoft Exchange

Powershell Commands to Interact with Microsoft Exchange

Connect, via Powershell, to Exchange Server remotely

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://server.domain.dom/PowerShell/ -Authentication Kerberos
import-pssession $session

Add Exchange Module

must already have Exchange Management Console installed

Exchange 2007

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin; 

Exchange 2010

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;

Exchange 2013 & 2016

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

Force update of Global Address Book

Get-GlobalAddressList | update-GlobalAddressList
Get-AddressList | update-AddressList
Get-OfflineAddressBook | Update-OfflineAddressBook

Change search scope to Entire forest

Set-AdServerSettings -ViewEntireForest $true

Note: you can check the status by running ‘get-adserversettings’

Set auto reply for a user

Set-MailboxAutoReplyConfiguration USERNAME -AutoReplyState enabled -ExternalAudience all -InternalMessage "This account is no longer in use. Please direct all future correspondence to USERNAME@DOMAIN.COM" -ExternalMessage "This account is no longer in use. Please direct all future correspondence to USERNAME@DOMAIN.COM"

Set forward and delivery (so that auto reply can still work)

Set-Mailbox <alias> -ForwardingAddress <recipient> -DeliverToMailboxAndForward $True

List all users with specific pattern in Primary Email address

get-mailbox | select PrimarySmtpAddress | where {$_.PrimarysmtpAddress -like "*rolling*"}

List all email addresses

get-recipient -resultsize unlimited | select name, emailaddresses

List all Distribution Lists and Members

$dist = foreach ($group in (Get-DistributionGroup -Filter {name -like "*"})) {Get-DistributionGroupMember $group | Select @{Label="Group";Expression={$Group.Name}},@{Label="User";Expression={$_.Name}},SamAccountName} $dist | Sort Group,User | Export-csv C:\Services\DL-export.csv -NoTypeInformation

List all public folders

get-publicfolder -identity "\" -recurse

Email enable a public folder

Enable-MailPublicFolder -identity "\PublicFolderPath\PublicFolder"

note: If you want to rcv emails from the outside, you will have to allow anonymous to the public folder and via the DL

List all accounts with Forwarding Address set

Get-mailbox | select DisplayName,ForwardingAddress | where {$_.ForwardingAddress -ne $Null}

Note: make sure to “Change search scope to ‘Entire Forest’”

List all Exchange attributes in AD user account

Get-AdUser Username -Properties * | Select *MSExch*

Search all mailboxes for specific text string

The following would search all subject, body, and attachments for “9986” and collect results in “john.doe” mailbox in a folder called “cc-9986”

get-mailbox -resultsize unlimited | search-mailbox -searchquery "9986" -targetmailbox "john.doe" -targetfolder "cc-9986"

Dynamic Distribution Lists

Create

Get Details

Get-DynamicDistributionGroup <name> | Format-List Name,RecipientFilter

Modify

  Set-DynamicDistributionGroup "Washington Management Team" -RecipientFilter {((RecipientType -eq 'UserMailbox') -and (Title -like 'Director*' -or Title -like 'Manager*') -and (StateOrProvince -eq 'WA') -and (Alias -ne $null) -and -not (Name -like "FederatedEmail*"))}

List database and mail server of input list of users (pull from AD)

'Rohit','Prateek','Sumit','Ankit' | %{ Get-ADUser  -Filter {name -eq $_} -Properties *} | ft Name, @{name='Exchange DB';expression={(($_.HomeMDB).split(',')[0]).split('=')[1]}} ,@{name='Exchange HomeServer';expression={($_.msExchHomeServerName -split 'Servers/cn=')[1]}} -AutoSize

Note: make sure to “Change search scope to ‘Entire Forest’”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment