azure AD module for windows powershell

Managing Office 365 Using Azure Active Directory Module for Windows PowerShell

The Office 365 platform includes a PowerShell module that allows you to manage Office 365 organization settings, configure users and licenses from the Command prompt. As with Exchange, there are several things that you can’t simply perform from the GUI in Office 365. To connect to Office 365 from PoSh CLI, you need to install additional software.

As a workstation, only 64 bit versions of Windows 10, 8.1, Windows 7 SP1 or Windows Server 2016/2012 R2 / 2012 / 2008R2 with .NET Framework version not lower than 3.5.1 can be used.

  1. Download and installMicrosoft Online Services Sign-In Assistant (
  2. Install the module Azure Active Directory Module for Windows PowerShell from the NuGet or PowerShellGallery repository using the command:
    Find-Module -Name MSOnline | Install-Module -Force

    windows azure active directory module for windows powershell

Now you can connect to your Office 365 subscription in Azure Active Directory.

Run the PowerShell console and import the module into your session:

Import-Module MSOnline

If you are using Multifactor Authentication (MFA), run the command


In the Azure Active Directory PowerShell window that appears enter the username (use full UPN – User principal name) and the password for Office 365, and enter the confirmation code from your phone.

Note. Your account must be a member of the administrative role of Office 365.

azure active directory module for windows powershell

For usual authentication by the username and password, first save the Office 365 administrative credentials to a PoSh variable:

$o365Credential = Get-Credential

And then connect to your subscription with the command:

Connect-MsolService -Credential $o365Credential

Upon completion of the command, a connection with your Office 365 tenant will be established.

The list of accepted domains in your organization can be displayed by the command:


List the available licenses in your account:


The cmdlet returns all SKUs belonging to the organization. You will receive detailed licensing information, find out how many licenses are available and how much is used.

Now display the list of Office 365 users:


As you can see, the list indicates whether the license is assigned to the user or not.

office 365 powershell module

You can check the user license type:

GetMsolUser –UserPrincipalName

You can create a new user using the New-MsolUser cmdlet:

New-MsolUser -UserPrincipalName -DisplayName 'Andy Hornet' -FirstName Andy -LastName Hornet' -LicenseAssignment theitbros:ENTERPRISEPACK -UsageLocation US

If you do not specify the -Password argument when creating a user, a random password will be generated and assigned for the user.

To set a password that will never expire for Office 365 user (not recommended), you should run the following command:

Get-MSOLUser –UserPrincipalName| Set-MsolUser -PasswordNeverExpires $true

For the convenience for choosing a specific license, use this command:

$LicenseSKUid = Get-MsolAccountSku | Out-GridView -Title 'Select a license type’-OutputMode Single | Select-Object -ExpandProperty AccountSkuId

List the users who are not assigned a license:

$Users = Get-MsolUser -All -UnlicensedUsersOnly

Assign usage location for the users that was found:

$Users | Set-MsolUser -UsageLocation US

Now you can assign licenses to all selected users:

$Users | Set-MsolUserLicense -AddLicenses $LicenseSKUid

You may also like:

Add Calendar Permissions in Office 365 via Powersh... This is a tutorial on how to add calendar permissions in Office 365 for your users via Powershell. You can add permissions onto a specific mailbox, or...
Office 365 – “This message could not b... Error Message: This message could not be sent. Try sending the message again later, or contact your network administrator. Error is . You might be rec...
TheITBros Microsoft Office 365 Migration Guide We just migrated a company to Microsoft Office 365 and we thought we would share our experience and troubleshooting tips. We hope this saves you some ...
Don’t Archive Notes in Office 365 Are your notes suddenly disappearing in Office 365? That's probably because you have enabled archiving in the Exchange Admin center with a retention p...
Disable Autodiscover on Exchange 2007 Server This tutorial will show you how to disable autodiscover on an Exchange 2007 on-premise server. Microsoft says this can't be done, but the following st...
READ ALSO  How to Extract Group Members from Active Directory and Export It to CSV file

Add Your Comment