Difference between revisions of "Most useful PowerShell commands for Exchange"

From Tech-Wiki
Jump to: navigation, search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Microsoft]]
 
[[Category:Microsoft]]
 
'''[[Microsoft#Misc|Back to Misc]]'''
 
'''[[Microsoft#Misc|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
 
Listing User
Line 19: Line 36:
 
  Import-PSSession $Session
 
  Import-PSSession $Session
  
Or for Office 365 use this URL instead:
+
Or for Office 365 use this -ConnectionUri instead:
 
  https://outlook.office365.com/powershell-liveid/ -Authentication Basic -Credential $UserCredential -SessionOption $ProxyOptions
 
  https://outlook.office365.com/powershell-liveid/ -Authentication Basic -Credential $UserCredential -SessionOption $ProxyOptions
  
'''Assign Mailbox Permissions'''
+
'''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 calendar permissions:
Line 28: Line 48:
  
 
Assign “Full Access” permissions for a Mailbox
 
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
 
  Add-MailboxPermission John -User Suzan -AccessRights FullAccess -InheritanceType All
  
 
Assign “Send As” Permissions for a Mailbox
 
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
 
  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
 
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  
 
  $DL = Get-DistributionGroupMember DL-01  
  Foreach ($item in $DL)  
+
  Foreach ($item in $DL)  { Add-RecipientPermission $item.name -AccessRights SendAs –Trustee Suzan }
  {  
+
Add-RecipientPermission $item.name -AccessRights SendAs –Trustee Suzan -Confirm:$False}
+
  
 
Sets email forwarding on mailbox:
 
Sets email forwarding on mailbox:
 
  Set-Mailbox -Identity “MailboxName” -ForwardingAddress “[email protected]” -DeliverToMailboxAndForward $true
 
  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
 
Revoke “Full Access” Permissions
PowerShell command Syntax
 
Remove-MailboxPermission <Identity> -User <Identity> -AccessRights FullAccess
 
PowerShell command Example
 
 
  Remove-MailboxPermission John -User Suzan -AccessRights FullAccess
 
  Remove-MailboxPermission John -User Suzan -AccessRights FullAccess
Adjustments & Improvements
+
 
To avoid the need for confirmation, we can add the option: “-Confirm:$False”
+
'''Misc'''
Remove-MailboxPermission John -User Suzan -AccessRights FullAccess -Confirm:$False
+
  
 
Migration statistics
 
Migration statistics
Line 94: Line 78:
 
Export Mailbox to PST
 
Export Mailbox to PST
 
  New-MailboxExportRequest -Mailbox User01 -FilePath ‘\\SERVER01\PSTFileShare\User01_Recovered.pst’
 
  New-MailboxExportRequest -Mailbox User01 -FilePath ‘\\SERVER01\PSTFileShare\User01_Recovered.pst’
 +
 +
'''Office 365'''
  
 
Create Office 365 Users
 
Create Office 365 Users
Line 100: Line 86:
 
  -UserPrincipalName &lt;Account&gt; -UsageLocation &lt;CountryCode&gt; -LicenseAssignment &lt;AccountSkuID&gt; [-Password &lt;Password&gt;]
 
  -UserPrincipalName &lt;Account&gt; -UsageLocation &lt;CountryCode&gt; -LicenseAssignment &lt;AccountSkuID&gt; [-Password &lt;Password&gt;]
  
Create Office 365 Users in Bulk mode
+
Create Office 365 Users in Bulk mode (from CSV file)
 
  UserPrincipalName,FirstName,LastName,DisplayName,UsageLocation,AccountSkuId
 
  UserPrincipalName,FirstName,LastName,DisplayName,UsageLocation,AccountSkuId
 
  [email protected],Claude,Loiselle,Claude Loiselle,US,contoso:ENTERPRISEPACK
 
  [email protected],Claude,Loiselle,Claude Loiselle,US,contoso:ENTERPRISEPACK
Line 110: Line 96:
 
  -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId [-Password $_.Password]}  
 
  -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId [-Password $_.Password]}  
 
  | Export-Csv -Path &lt;Output CSV File Path and Name&gt;
 
  | Export-Csv -Path &lt;Output CSV File Path and Name&gt;
 
 
  
 
Office 365 commands:
 
Office 365 commands:
 
  https://blog.netwrix.com/2018/09/19/ten-most-useful-office-365-powershell-commands/
 
  https://blog.netwrix.com/2018/09/19/ten-most-useful-office-365-powershell-commands/

Latest revision as of 02:50, 26 October 2023

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/