PowerShell – Microsoft 365 User and Assigned Licenses Report

Microsoft 365 provides a lot of well conceived admin tools via its on-line portal. Their user tool is fantastic for creating and editing user accounts, but getting reports from it can be challenging. Lucky for us Microsoft 365 can also be accessed through PowerShell.

This script will prompt for credentials and the org name. Then it will retrieve all of the licensed users’ display names, primary smtp addresses from Exchange, user principle names, and the licenses currently applied to the account. The CSV report will be saved in your documents library.

The machine you run the script from needs the MS365 PowerShell modules installed. See https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell

$UserCredential = Get-Credential
$ExchangeOnline = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
	Import-PSSession $ExchangeOnline
Connect-MsolService -Credential $UserCredential
$tenant = Read-Host "Enter Tenant Org Name"
$tenantname = $tenant + ":"

Get-MsolUser -All | Where {$_.islicensed -like "True"} | Select DisplayName, UserPrincipalName,
    @{Name="Primary Email Address"; Expression={Get-Mailbox -Identity $_.UserPrincipalName | Select-Object PrimarySmtpAddress -ExpandProperty PrimarySmtpAddress}},
    @{Name="Enabled Licenses"; Expression={[string]($_.Licenses.AccountSkuID).Replace("$tenantname", "     ") -replace "_", " "}}| 
    Export-Csv -Path $env:USERPROFILE\documents\Microsoft_365_User_Report.csv -NoTypeInformation    

Office 365 All-in-One PowerShell Management Console

If you administrate an Office 365 tenant, you’ve undoubtedly discovered that PowerShell is a requirement rather than an option. Using PowerShell with Office 365 isn’t all that different from the on-premises version, but connecting to all the services can be challenging. Often, a task or project requires multiple modules to function.

With a little scripting knowledge we can connect to and manage all of the O365 services at once. I can’t tell you how many times I got half-way through a project only to realize that I didn’t have all the required cmdlets. Logging on to everything each time I use PowerShell saves time and frustration.

To make the code below work, install the PowerShell modules each service requires. Open an elevated PowerShell console (right click, run as administrator). Check the execution policy (Get-ExecutionPolicy), if it is restricted use the following command to change that: Set-ExeuctionPolicy -ExecutionPolicy Unrestricted . Then use the following commands to install all of the tools:

  • Install-Module AzureAD
    • Type A and press Enter (yes to all) when prompted.
  •  Install-Module MSOnline
    • Type A and press Enter (yes to all) when prompted.
  • Install-Module MicrosoftTeams
    • Type A and press Enter (yes to all) when prompted.
  • Install-Module Microsoft.SharePoint.Online.PowerShell
    • Type A and press Enter (yes to all) when prompted.

Copy the code below and save it as a PS1 file named Manage-O365.PS1 in your documents folder. Make a new desktop shortcut with the following path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit “C:\Users\profile\Documents\Manage-O365.ps1” . When you double-click it you’ll be prompted for credentials and the Office 365 organization name. After entering them, a PowerShell console will launch and connect to O365 services. The window stays open until you are done with your tasks.

$UserCredential = Get-Credential
$OrgName = Read-Host "Enter the Name of your Office 365 Organization, Example: Techbloggingfool"
	$ExchangeOnline = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
		Import-PSSession $ExchangeOnline
	Connect-MsolService -Credential $UserCredential
	Connect-AzureAD -Credential $UserCredential
	Connect-MicrosoftTeams -Credential $UserCredential
	Connect-SPOService -Url https://$OrgName-admin.sharepoint.com -Credential $UserCredential

Tip: Save these lines as a snippet in your favorite IDE (Visual Code, ISE, etc.) and you can easily insert them for Office 365 scripting projects.