Most useful PowerShell commands for Exchange

From Tech-Wiki
Revision as of 21:27, 6 June 2019 by Fabricio.Lima (Talk | contribs)

Jump to: navigation, search

Back to Misc

Users and Server

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 URL instead:

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

Assign Mailbox Permissions

Add calendar permissions:

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

Assign “Full Access” permissions for a Mailbox

PowerShell command Syntax
Add-MailboxPermission <Identity> -User <Identity> -AccessRights FullAccess -InheritanceType All
PowerShell command Example
Add-MailboxPermission John -User Suzan -AccessRights FullAccess -InheritanceType All

Assign “Send As” Permissions for a Mailbox

PowerShell command Syntax
Add-RecipientPermission <Identity> -AccessRights SendAs -Trustee <Identity>
PowerShell command Example
Add-RecipientPermission John -AccessRights SendAs -Trustee Suzan
Adjustments & Improvements
To avoid the need for confirmation, we can add the option: “-Confirm:$False”
Add-RecipientPermission John -Trustee Suzan -AccessRights SendAs -Confirm:$False

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

PowerShell command Syntax
$DL = Get-DistributionGroupMember 
Foreach ($item in $DL) 
{ 
Add-RecipientPermission $item.name -AccessRights SendAs
–Trustee <Identity> -Confirm:$False 
}
PowerShell command Example
$DL = Get-DistributionGroupMember DL-01 
Foreach ($item in $DL) 
{ 
Add-RecipientPermission $item.name -AccessRights SendAs –Trustee Suzan -Confirm:$False}

Sets email forwarding on mailbox:

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

Display permissions for a Mailbox

Display “Full Access” Permissions for a Mailbox

PowerShell command Syntax
Get-MailboxPermission <Identity>
PowerShell command Example
Get-MailboxPermission John
Adjustments & Improvements
For improving the quality of the output we can use an additional PowerShell parameter that will “clean” the unnecessary information:
Get-MailboxPermission John | Where { ($_.IsInherited -eq $False) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | Select Identity,user,AccessRights

Revoke Permissions

Revoke “Full Access” Permissions

PowerShell command Syntax
Remove-MailboxPermission <Identity> -User <Identity> -AccessRights FullAccess
PowerShell command Example
Remove-MailboxPermission John -User Suzan -AccessRights FullAccess
Adjustments & Improvements
To avoid the need for confirmation, we can add the option: “-Confirm:$False”
Remove-MailboxPermission John -User Suzan -AccessRights FullAccess -Confirm:$False

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

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/