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.
- Download and install Microsoft Online Services Sign-In Assistant (https://www.microsoft.com/en-us/download/details.aspx?id=41950)
- 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
Now you can connect to your Office 365 subscription in Azure Active Directory.
Run the PowerShell console and import the module into your session:
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.
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 type in and run command Get-MsolUser.
As you can see, the list indicates whether the license is assigned to the user or not.
You can check the user license type:
GetMsolUser –UserPrincipalName email@example.com
You can create a new user using the New-MsolUser cmdlet:
New-MsolUser -UserPrincipalName firstname.lastname@example.org -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 email@example.com| 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