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.
First, we need 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
How has your experience been with connecting PowerShell to Office 365? Or perhaps, connecting to Exchange Online? Join the conversation on Twitter @SuperTekBoy.
i would like to train in office365 administrator , please advice on how o go about it.
Gareth Gudger says
There are a lot of options out there.
You can find free video training resources through Microsoft’s Channel 9 (https://channel9.msdn.com/), the Microsoft Virtual Academy (https://mva.microsoft.com/), or Microsoft Learn (https://docs.microsoft.com/en-us/learn/).
There are also paid video courses available through PluralSight or LinkedIn Learning.
You can look into Microsoft Certifications – https://www.microsoft.com/en-us/learning/default.aspx
Hope this helps.