Most useful PowerShell commands for Exchange

From Tech-Wiki
Jump to: navigation, search

Back to Misc

Load the Powershell module

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Add-PSSnapIn -Name Microsoft.Exchange, Microsoft.Windows.AD

Restart DAG server

Move-ActiveMailboxDatabase –Server ToBeRebootedServer
Get-MailboxDatabaseCopyStatus *

Restart single server

Get-service | ?{$_.Name -ilike "MSexch*"} | stop-service


Users and Server

List user's LastLogon

Get-ADUser -Identity sp13_admin -Properties LastLogon | Select Name, @{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}

Listing User

Get-Mailbox -identity “MailboxName” | fl
Get-Mailbox | Select DisplayName, SamAccountName, UserPrincipalName, PrimarySMTPAddress
Get-MailboxStatistics -Database (DM) | Select DisplayName, TotalItemSize
Get-Mailbox | Where-Object {$_.displayname -like ‘*User*Name*‘} | fl
Get-MailboxStatistics  -Filter ‘displayName -eq “NameOfMailbox“‘ | fl

Listing server/mailboxes

Get-ExchangeServer | fl
Get-MailboxDatabase | fl
Get-MailboxDatabase -Identity “MailboxDatabase” -Server “Server” -Status | Format-List

Open remote connection to Exchange server

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServer/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session

Or for Office 365 use this -ConnectionUri instead:

https://outlook.office365.com/powershell-liveid/ -Authentication Basic -Credential $UserCredential -SessionOption $ProxyOptions

Mailbox Permissions

Display “Full Access” Permissions for a Mailbox

Get-MailboxPermission John | Where { ($_.IsInherited -eq $False) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | Select Identity,user,AccessRights

Add calendar permissions:

Add-MailboxFolderPermission -Identity “MailboxName:\Calendar” -User UserRequiringAccess -AccessRights Reviewer

Assign “Full Access” permissions for a Mailbox

Add-MailboxPermission John -User Suzan -AccessRights FullAccess -InheritanceType All

Assign “Send As” Permissions for a Mailbox

Add-RecipientPermission John -AccessRights SendAs -Trustee Suzan

Assign “Send As” Permissions for recipient for each member in a distribution group

$DL = Get-DistributionGroupMember DL-01 
Foreach ($item in $DL)  { Add-RecipientPermission $item.name -AccessRights SendAs –Trustee Suzan }

Sets email forwarding on mailbox:

Set-Mailbox -Identity “MailboxName” -ForwardingAddress “[email protected]” -DeliverToMailboxAndForward $true

Revoke “Full Access” Permissions

Remove-MailboxPermission John -User Suzan -AccessRights FullAccess

Misc

Migration statistics

Get-MigrationUser -BatchId StagedBatch1 | Get-MigrationUserStatistics

Message Tracking log

Get-MessageTrackingLog -Start "02/26/2018 08:23:00" -End "02/28/2018 17:00:00" -Recipients $recipientSMTP -Server $ExchServer

Set Auto-Reply

Set-MailboxAutoReplyConfiguration -Identity "Desmond Miles" -AutoReplyState Enabled `
-InternalMessage "I'm currently on leave until 23th April. Please contact Ezio Auditore on x72023 for urgent matters."
AutoReplyState can also be: AutoReplyState Scheduled –StartTime “02/28/2018 07:00:00” –EndTime 03/18/2018 17:00:00

Export Mailbox to PST

New-MailboxExportRequest -Mailbox User01 -FilePath ‘\\SERVER01\PSTFileShare\User01_Recovered.pst’

Office 365

Create Office 365 Users

New-MsolUser -UserPrincipalName "[email protected]" -DisplayName " Desmond Miles " -FirstName "Desmond" -LastName "Miles" -UsageLocation "CH" -LicenseAssignment "Contoso:BPOS_Standard"
New-MsolUser -DisplayName <DisplayName> -FirstName <FirstName> -LastName <LastName> 
-UserPrincipalName <Account> -UsageLocation <CountryCode> -LicenseAssignment <AccountSkuID> [-Password <Password>]

Create Office 365 Users in Bulk mode (from CSV file)

UserPrincipalName,FirstName,LastName,DisplayName,UsageLocation,AccountSkuId
[email protected],Claude,Loiselle,Claude Loiselle,US,contoso:ENTERPRISEPACK
[email protected],Lynne,Baxter,Lynne Baxter,US,contoso:ENTERPRISEPACK
[email protected],Shawn,Melendez,Shawn Melendez,US,contoso:ENTERPRISEPACK
Import-Csv -Path <Input CSV File Path and Name> | 
foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName 
-UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId [-Password $_.Password]} 
| Export-Csv -Path <Output CSV File Path and Name>

Office 365 commands:

https://blog.netwrix.com/2018/09/19/ten-most-useful-office-365-powershell-commands/