Connect PowerShell to Office 365 cloud

We do quite a few Office 365 migrations and recently found out you can connect PowerShell to the Office 365 cloud and do quite a lot of tasks that way instead of using their web interface. This is going to make our jobs much easier. I will show you how to connect to 365 and some examples of things you can do. Some of the more powerful scripts such as user control require you to install the Office 365 sign in tool and the Microsoft Online PowerShell snap-in.

To be able to run commands that affect user accounts you must install the Microsoft Online Services Module for PowerShell which can be found here which also requires the single sign on tool located here.

To connect to Office 365 with PowerShell run the following 4 commands. You will be prompted for your account credentials.

set-executionpolicy remotesigned
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
To connect to the user management service issue the following
To set a users password to something you know and set them to not have to change it
Set-MsolUserPassword -UserPrincipalName CHANGETO@USERNAME -NewPassword SETTHISTOTHENEWPW -ForceChangePassword $false
To set all users to not have to change password every 90 days
Get-MsolUser | Set-MsolUser –PasswordNeverExpires $True

No comments:

Post a Comment