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

From Tech-Wiki
Jump to: navigation, search
 
(8 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'''
 
'''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 21: 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 30: 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
  
Line 39: Line 54:
  
 
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
 
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
 
  Remove-MailboxPermission John -User Suzan -AccessRights FullAccess
 
  Remove-MailboxPermission John -User Suzan -AccessRights FullAccess
 +
 +
'''Misc'''
  
 
Migration statistics
 
Migration statistics
Line 86: 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

Latest revision as of 01: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/