How to Connect to Office 365 using Powershell (2024)

How to Connect to Office 365 using Powershell. Have you ever found yourself needing to manage Office 365 users, groups, and services, but the web based admin center just doesn’t cut it? In this situation, PowerShell is a great help. In this guide, I’ll teach you how to connect to Office 365 with PowerShell in two simple steps.

After, I will show you how to display all available commands for managing O365, and finally, I have some examples of tasks you can perform in Office 365 with PowerShell.

Let’s start.

Also ReadGet-AzureADAuditSignInLogs – Find Sign In Logs Last 30 Days PowerShell

How to Connect to Office 365 using Powershell

Step 1: Install the MSOnline PowerShell Module

As mentioned in my introduction, the first step to connecting to Office 365 via PowerShell is to install the MSOnline module.

Follow the steps below to install the module:

1. Search powershell, then, on the search result, with Windows PowerShell selected, click Run as Administrator.

Windows will prompt you to allow the app to make changes to your PC; click Yes.

2. When the PowerShell command console opens, run the command below.

The command opens your PowerShell session with the Unrestricted ExecutionPolicy. This allows you to run commands from the module you will download later.

powershell.exe -ExecutionPolicy Unrestricted

3. Then, to install the MSOnline PowerShell module, enter the command below and press the Enter.

Import-Module MSOnline

The command may take a while to run. When it finishes, the PowerShell console returns to the prompt.

4. Before you proceed, confirm that the module has been installed on your computer by running the command below:

Get-Module MSOnline

If you successfully installed the MSOnline module on your computer, the above command displays results similar to what is in the screenshot below.

5. Finally, to display all the cmdlets (commandlets) in the MSOnline module, run the Get-Module.

The command returns all the cmdlets you can run once you connect to Office 365. The screenshot below shows the cmdlets in the MSOnline PowerShell module.

Note that some of the cmdlets are not shown in my screenshot.

Also ReadTop 15 Office 365 PowerShell Commands (Users, Groups, Licensing)

Step 2: Connect To Office 365 With the Connect-MsolService Command

Once you have installed the MSOnline module and confirmed that the cmdlets are available on your computer, proceed with the steps below to connect to Office 365.

1. Encrypt

Encrypt your Office 365 credentials and save it in a file called Office365.XML by running the command below:

Change UserName@DomainName.com to your Office 365 user name. The name MUST be specified in the UPN format, UserName@DomainName.com. Also, change the path – D:\PowerShellCred\ – to a path on your local computer.

Get-Credential UserName@DomainName.com | Export-CliXml -Path D:\PowerShellCred\Office365.XML

When you run the above command, Windows PowerShell requests that you enter the account’s password. Enter the password and click OK.

AlsoHow to Check if MFA is Enabled in Office 365 for Users

2. Decrypt

The next step is to decrypt the password and save it in a variable by running the Import-Clixml command as shown below:

$ImpCred = Import-Clixml D:\PowerShellCred\Office365.XML -ErrorAction SilentlyContinue

3. Connect

Finally, connect to your Office 365 tenant by running the Connect-MsolService PowerShell command below:

Connect-MsolService -Credential $ImpCred

If everything goes as expected, the command should run successfully, and PowerShell should return to its prompt.

Effectively, after running the command, it will appear that nothing happens. This means that you have successfully connected to Office 365 with PowerShell.

You can run available PowerShell commands from this point – see some examples in the next section.

Improve your Active Directory Security & Azure AD with Office 365 Reporting Tool

Try us out for Free, Access to all features. – 200+ AD Report templates Available. Easily customise your own AD reports.

Also ReadGet InfraSOS – Office 365 Reporting Tool

Examples of Commands You Can Run After Connecting To Office 365

Now that you’ve successfully connected to your Office 365, I show you common commands you can run.

Get-MsolUser

The Get-MsolUser command is one of the commands you will likely run in Office 365. The Get-MsolUser command returns all users in your Office 365 tenant.

If you have a large user base, DO NOT run this command without filtering, as it may take too long to return all users in your account.

The screenshot below shows the result of the command against my O365.

Earlier, I advised against running the Get-MsolUser command without any parameters.

If you look closely at my screenshot above, you notice that the command result includes an isLicensed column which includes the boolean value, True or False. Based on this, you can use this command to return all users in your Office 365 tenant that are licensed.

To return all licensed users, modify the Get-MsolUser command as shown below:

Get-MsolUser | Where-Object {$_.isLicensed -eq $True}

This time, the Get-MsolUser command returned only users that have been assigned an Office 365 license.

I hope you can clearly see the benefit of this command to you as an Office 365 Admin!

Before moving on to the next example, I share one more important task you can perform with the Get-MsolUser.

Use this command to get the Office 365 users that have been assigned a specific Office license type.

If you know the license type you want to report for, run the comma below. The command returns all users that have been assigned the “ENTERPRISEPACK” license.

Replace ENTERPRISEPACK with the license type you want to return users for.

The last command works if you already know the license type. However, if you do not know the type of license available in your Office 365, run the command below to return all available licenses.

Run the command
Get-MsolAccountSku

The command returns all Office 365 licenses available in your account. Also included with the result is the number of licenses available in your account (ActiveUnits column).

Additionally, you also get how many of the licenses have been used (the ActiveUnits column).

Once you have this information, you use the license name in the command below to return the users assigned to a particular license. Get the name of the license from the AccountSkuId column of the results of the last command.

Get-MsolUser | Where-Object { $_.Licenses[0].AccountSkuId -eq "ENTERPRISEPACK" } | Select-Object UserPrincipalName, DisplayName

For my Office 365, when I run the command below for a license type, it returns the result shown in the screenshot below:

Also ReadAzure AD Connect Force Sync PowerShell/ Synchronization Service Manager

Get-MsolDomain

Another important Office 365 command you can run is the Get-MsolDomain. As you would have guessed, this command returns all the domain names you have configured in your Office 365 account.

Here is the result of the command when I run it against my Office 365 account:

Also ReadGet-MgUser – Find and Export Azure AD Users with PowerShell

How to Connect to Office 365 using Powershell Conclusion

The Office 365 PowerShell module has a lot to offer. However, before you access the commands in the module, you have to connect to O365.

In this guide, I showed you the steps to connect to your Office 365 account with PowerShell and run come common commands.

Connecting to O365 via PowerShell is a simple two-step process:

  1. Install the MSOnline PowerShell module.
  2. Connect To Office 365 With the Connect-MsolService Command.

Once you have successfully connected, you list all the available cmdlets in the MSOnline PowerShell module by running the command below:

(Get-Module MSOnline | Select-Object ExportedCommands).ExportedCommands

How to Connect to Office 365 using Powershell (13)

Try InfraSOS for FREE

Invite your team and explore InfraSOS features for free

  • Free 15-Days Trial
  • Easy Setup
  • Full Access to Enterprise Plan
How to Connect to Office 365 using Powershell (2024)
Top Articles
How do I redeem a gift card if the digits…
Can a car key fob be hacked | Sure Lock & Key
Paperman And Sons Obituary
Pixel Combat Unblocked
Whiskeytown Camera
Local Body Rubs
Craigslist Personals Kenosha Wi
20 Inspiring Interiors That Display Brilliant Bathroom Lighting Ideas
Dom Tradingview
A Nearly Normal Family: Limited Series | Rotten Tomatoes
Freedom Powersports Mcdonough Photos
Cash App Paid Screenshot
Largo Clinic And Med Spa
Jail Roster Independence Ks
Villanova University Common Data Set
90 Days From February 28
Erica Mena Net Worth Forbes
Meet Our Doctors | Laveen dental Laveen, AZ
Wedding Dr Amy Hutcheson Married
Ac-15 Gungeon
Wbay Tv Weather Forecast
Putnam.schoology.com
Commercial Credit Cards for Business Expenses | J.P. Morgan
Electric Toothbrush Feature Crossword
Waters Funeral Home | Baldwyn, MS Funeral Home & Cremation
Paul Mccombs Nashville Tn
Monster Hunter Rise Steam Unlocked
Filmy4 Web.com
WeeCars, le réseau d'agences spécialisées dans les véhicules d'occasion - Le Quotidien des Entreprises
Farmers And Merchants Bank Broadway Va
69 Value Dollar Shop Singapore 2024: Opening Hours and Locations
Craigslist Pets Salina Ks
Best Restaurants In Ardmore Pa
Texas Longhorns Soccer Schedule
BMO Bank Review 2024
Chaithanya Matrimony Login
Aces Login Alamo
Effingham Bookings Florence Sc
Oreillys O Reilly Auto Parts
Sustainability Foundations Everfi Answers
Skipthe Games.com
Vip Market Vetsource
Hanco*ck County Mugshots Busted Newspaper
Why rivalry match between Pitt and Penn State volleyball is bigger than the Xs and Os
Canada Life Insurance Comparison Ivari Vs Sun Life
Craiglist Okaloosa
Fgo Spirit Root
R/Bayonetta
DER SPIEGEL zum Hören 39/2024
Costco Members: Any Set of 4 Michelin Tires $200 Off $900+
Myhr North Memorial
Palm Beach Post Obit
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6033

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.