Office 365 is great, but the Admin Center does not bode well for bulk tasks. What might take an insanely massive amount of time in the graphical user interface may only take seconds in PowerShell.
In the first part of this article, we discuss all the prerequisites required to make the connection. The second part discusses how to make the connection.
Before we can connect we need to download the Microsoft Online Services Sign-In Assistant. You can get that here: https://www.microsoft.com/en-us/download/details.aspx?id=41950
Once downloaded, run the executable. On the first screen click the Next button.
Check the box to accept the license agreement and click the Install button.
This is all we need to do with the Sign-in Assistant. Our second prerequisite is to install the MSOnline module in PowerShell. To do this launch PowerShell as an administrator.
Then type the following:
C:\> Install-Module -Name MSOnline
You may be prompted to install the NuGet provider if it is missing or out of date. Type Y and press enter to install NuGet.
NuGet provider is required to continue
PowerShellGet requires NuGet provider version '22.214.171.124' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\Administrator\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 126.96.36.199 -Force'.
Do you want PowerShellGet to install and import the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
You may be prompted to allow installation from an untrusted repository. With this being a known module from the PowerShell Gallery, type Y and press enter. However, it is always wise to scrutinize all modules, regardless of where they are sourced.
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet.
Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
With the module installed you will need to close any PowerShell windows.
Connect PowerShell to Office 365
Launch a new PowerShell window and run the Connect-MsolService command. You will be prompted to enter your credentials. Enter your username and password.
Note: If you have MFA enabled on your admin account you will receive additional security prompts, such as entering a code from a text message, or, responding to the Microsoft Authenticator app.
Once authenticated you will be returned to the PowerShell window where you can start using MSOnline PowerShell commands. In our example below we are running Get-MsolUser.
C:\> Get-MsolUser | Format-Table DisplayName
Sarah Jane Smith
You are all set!
- Easily Connect to Exchange Online with PowerShell
- Install Exchange 2016 in your lab (7-part series)
- Renew a Certificate in Exchange 2016