Common PowerShell Uses in Office 365
Common PowerShell Uses in Office 365
Uses
The following PowerShell commands have been used in an Exchange 2013 Hybrid Exchange
Online environment, where the authoritative identity remains on premise with Active Directory.
This should be used as a starter while different business scenarios may require different
attribute filtering or different options chosen to be configured. Below some commands are run
from on premise Exchange PowerShell and others are run from a PowerShell connected to
MSOL Service.
Lets get connected Exchange Online
Import-Module MSOnline
$O365Cred = Get-Credential
$O365Session = New-PSSession ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential
$O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session
Lets get connected Office 365
Connect-MsolService Credential $O365Cred
Lets get the Office 365 licenses for an individual user (PowerShell Connected to MSOL Service)
(Get-MSOLUser UserPrincipalName [email protected]).Licenses[0].ServiceStatus
Lets get the Office 365 licenses for our organization (PowerShell Connected to MSOL Service)
get-msoluser -all | ForEach-Object { "============="; $_.DisplayName; $_.Country; $_.licenses[0].servicestatus}
Lets bulk enable remote mailboxes and set with archive (On Premise Exchange PowerShell)
Get-user organizationalUnit domain.com/Asia Pacific/Vietnam/Users| where-object {$_.RecipientType eq User} | foreach {Enable-
RemoteMailbox -identity $_.userprincipalname -primarysmtpaddress ($_.firstname+'.'[email protected]) -
RemoteRoutingAddress ([email protected])} | set-remotemailbox -emailaddresspolicyenabled:$true
Get-user organizationalUnit domain.com/Asia Pacific/Vietnam/Users| where-object{$_.RecipientType eq User} | foreach {Set-
RemoteMailbox -identity $_.userprincipalname -archive}
Using a filter to prevent some accounts from receiving a remote mailbox while setting in bulk
(On Premise Exchange PowerShell)
Retrieve List of No MBX users
Get-user organizationalUnit domain.com/Europe/Austria/Users| where-object {$_.RecipientType eq User} | where { $_.notes -like "*No
MBX*" }
Enable Remote Mailbox and Archive
Get-user organizationalUnit domain.com/Europe/Austria/Users| where-object {$_.RecipientType eq User} | where { $_.notes -notlike
"*No MBX*" } | foreach {Enable-RemoteMailbox -identity $_.userprincipalname -primarysmtpaddress
($_.firstname+'.'[email protected]) -RemoteRoutingAddress
([email protected])} | set-remotemailbox -emailaddresspolicyenabled:$true
Get-user organizationalUnit domain.com/Asia Pacific/Vietnam/Users| where-object{$_.RecipientType eq User} | where { $_.notes -
notlike "*No MBX*" } | foreach {Set-RemoteMailbox -identity $_.userprincipalname -archive}
Lets set usage location, assign licenses, and set mailbox language to Office 365 users in bulk
(PowerShell Connected to MSOL Service)
Get-msoluser -maxresults 40000 | where-object{$_.Country eq Vietnam} | foreach {Set-MsolUser -UserPrincipalName
$_.UserPrincipalName -UsageLocation "VN"}
Get-msoluser -maxresults 40000 | where-object{$_.Country eq Vietnam} | foreach {Set-MsolUserLicense -UserPrincipalName
$_.UserPrincipalName -AddLicenses 'domain:ENTERPRISEPACK'}
Get-msoluser -maxresults 40000 | where-object{$_.Country eq Germany} | foreach {set-MailboxRegionalConfiguration -Identity
$_.UserPrincipalName -Language de-de -DateFormat "dd.MM.yyyy" -TimeFormat HH:mm -LocalizeDefaultFolderName:$True}
Lets get the Encryption Configuration (PowerShell Connected to Exchange Online Service)
Get-OMEConfiguration