Office 365 PowerShell

Running PowerShell for Office 365 allows you to tweak additional settings for cloud based accounts. There are several pre-requisites that need to occur prior to running Office 365 PowerShell commands / cmdlets.

You will need to install the Microsoft Online PowerShell module from Microsoft. More information on how to install and configure Windows PowerShell can be found here.

Once you have installed the module, run the following:
Import-module msonline
$LiveCred=Get-Credential
Connect-MsolService –Credential $LiveCred

Run the following 3 commands to connect Windows PowerShell to the Office 365 exchange service:

  • $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ –Credential $LiveCred -Authentication Basic –AllowRedirection
  • Import-PSSession $Session (If you get an error with this cmdlet, run this Set-ExecutionPolicy Unrestricted)

To change the UserPrincipalName (UPN) for a user in the cloud even while the directory sync is still running:
set-msoluserprincipalname –UserPrincipalName old_user.name@domain.com.au -NewUserPrincipalName new_user.name@domain.com.au

For a reference to available PowerShell cmdlets in Exchange Online, see Reference to Available PowerShell Cmdlets in Exchange Online.

Also see Exchange Online PowerShell for the official way to connect using PowerShell.


Some examples of PowerShell commands are as follows:

To get a list of all deleted users in the cloud:
Get-MsolUser –ReturnDeletedUsers

To remove a user from deleted items in the cloud:
Remove-MsolUser -UserPrincipalName example@abc.com –RemoveFromRecycleBin

To remove a user from deleted items in the cloud:
Set-Mailbox user.name -EmailAddresses @{remove="address@<domain>.onmicrosoft.com"}

To disable directory synchronization:
Set-MsolDirSyncEnabled -EnableDirSync $false
You may get a message in on the portal that says Active Directory synchronization is being deactivated. This process may take up to 72 hours to complete. It only took about 5 minutes for me, then I was allowed to modify entries in the cloud without an error message saying The action can’t be performed on the object ‘user.name’ because the object is being synchronized from your on-premises organization.

To re-enable directory synchronization:
Set-MsolDirSyncEnabled -EnableDirSync $true

To check directory synchronization:
(Get-MSOLCompanyInformation).DirectorySynchronizationEnabled

To disable the requirement for strong password:
Set-MsolUser -UserPrincipalName [UserPrincipalName] -StrongPasswordRequired $False
Set-MsolUserPassword -UserPrincipalName [UserPrincipalName] -NewPassword [NewPassword] -ForceChangePassword $false

To disable the requirement for strong password for all users:
Get-MsolUser | Set-MsolUser -StrongPasswordRequired $False

To check directory synchronization:
(Get-MSOLCompanyInformation).DirectorySynchronizationEnabled

To disable password expiry for an individual user, run the following code:
Set-MsolUser –UserPrincipalName <UPNName> -PasswordNeverExpires $True

To disable password expiry for all of your users, run the following code:
Get-MsolUser | Set-MsolUser –PasswordNeverExpires $True


This is a really good reference to perform a staged Exchange migration to Office 365. This article has a whole bunch of really good information which is very easy to follow.

One Comment

  1. I simply couldn’t go away your site prior to suggesting that I really enjoyed the standard info a person supply in your visitors? Is going to be back regularly in order to check up on new posts

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s