Office 365 licenses, PowerShell and the Kitchen Sink

How many times have you logged into Office 365 via PowerShell and had someone interrupt your work to request information about O365 licensing, only to discover that gathering this info via PowerShell is not the most pleasant experience?

Some of you may be thinking, “Well, I can just go to the portal and get the information by creating a filter for the license type, etc.” Great! But what if you don’t want to take the extra time it takes to login, navigate, etc.? As you may already know, the information is accessible via PowerShell; but did you know you can gather the information they need, right in PowerShell, without having to open a web browser?

The following commands need to execute while you have an active connection to Azure AD. If you need instructions on how to do this, Microsoft provides a guide here.

How to pull license information via PowerShell:

The following are some simple one-liners to get at the information quickly. I converted them into functions for quick use, and to enable you to incorporate them into any existing function rich script, etc. But this is by no means essential.

Oh, and if you’re confused about the SKUs, here’s a site that helps you decipher them.

Commands

This example gathers all the available SKUs in your tenant:

Get-MsolAccountSku | Select AccountSkuID, SkuPartNumber, ActiveUnits, WarningUnits, ConsumedUnits | FT -AutoSize

Export the same information to CSV:

Get-MsolAccountSku | Select AccountSkuID, SkuPartNumber, ActiveUnits, WarningUnits, ConsumedUnits | Export-Csv FileName.csv -NoTypeInformation

This example gathers all licensed users with the corresponding license in use:

Get-MsolUser -All | where {$_.isLicensed -eq $true}  | Select-Object DisplayName, UserPrincipalName, @{name="Licenses";expression={$_.licenses.accountskuid}}

Export the same information to CSV:

Get-MsolUser -All | where {$_.isLicensed -eq $true}  | Select-Object DisplayName, UserPrincipalName, @{name="Licenses";expression={$_.licenses.accountskuid}} | Export-Csv FileName.csv -NoTypeInformation

This example gathers all the unlicensed users:

Get-MsolUser -All -UnlicensedUsersOnly | Select DisplayName, UserPrincipalName

Export the same information to CSV:

Get-MsolUser -All -UnlicensedUsersOnly | Select DisplayName, UserPrincipalName | Export-Csv FileName.csv -NoTypeInformation

While the previous commands accomplish their tasks, what if you want a function?

What is a PowerShell function, you ask?

PowerShell Functions

In PowerShell 6 About Functions, Microsoft describes a function as “a list of PowerShell statements that has a name that you assign. When you run a function, you type the function name. The statement in the list run as if you had typed them as the command prompt.”

I create functions for commands I run often. You may take this a step further and create a full module with all your functions. Writing your own PowerShell module is beyond the scope of this post, but it is possible. If you’re interested, check out Microsoft’s How to Write a PowerShell Script Module.

Get-TenantLicenses – The function below may be utilized to display the licenses available to the tenant:

Function Get-TenantLicenses { Get-MsolAccountSku | Select AccountSkuID, SkuPartNumber, ActiveUnits, WarningUnits, ConsumedUnits | FT -AutoSize  }

Once the function has been executed, you can simply run Get-TenantLicenses to execute everything in the function.

Export-TenantLicense -Export filename.csv  – The function below may be utilized to export the licenses available to the tenant in CSV format:

Function Export-TenantLicenses ($Export) { Get-MsolAccountSku | Select AccountSkuID, SkuPartNumber, ActiveUnits, WarningUnits, ConsumedUnits | Export-Csv $Export -NoTypeInformation }

Once the function is executed, you can simply run Export-TenantLicense -Export filename.csv to execute everything in the function and export a file with the name you specify.

Get-TenantUserLicenses – The function below may be utilized to display tenant users licensed with the corresponding license:

Function Get-TenantUserLicenses { Get-MsolUser -All | where {$_.isLicensed -eq $true}  | Select-Object DisplayName, UserPrincipalName, @{name="Licenses";expression={$_.licenses.accountskuid}} }

Once the function is executed, you can simply run Get-TenantUserLicenses to execute everything in the function.

Export-TenantUserLicense -Export Filename.csv – The function below may be utilized to export the tenant users licensed with the corresponding license:

Function Export-TeanantUserLicenses ($Export) { Get-MsolUser -All | where {$_.isLicensed -eq $true}  | Select-Object DisplayName, UserPrincipalName, @{name="Licenses";expression={$_.licenses.accountskuid}} | Export-Csv $Export -NoTypeInformation }

Once the function is executed, you can simply run Export-TenantUserLicense -Export Filename.csv to execute everything in the function and export a file with the name you specify.

Get-TenantUserNoLicenses  – The function below may be utilized to display unlicensed tenant users:

Function Get-TenantUserNoLicenses { Get-MsolUser -All -UnlicensedUsersOnly | Select DisplayName, UserPrincipalName }

Once the function is executed, you can simply run Get-TenantUserNoLicenses to execute everything in the function.

Export-TenantNoUserLicense -Export Filename.csv  – The function below may be utilized to export unlicensed tenant users:

Get-MsolUser -All -UnlicensedUsersOnly | Select DisplayName, UserPrincipalName | Export-Csv FileName.csv -NoTypeInformation

Once the function is executed, you can simply run Export-TenantNoUserLicense -Export Filename.csv to execute everything in the function and export a file with the name you specify.

Modifiying the above functions to suit your unique needs can save time and cut down on the distractions that go along with opening a browser. 😊 If you enjoyed this post, Anexinet offers many additional Office 365-related posts, available here. Further, Anexinet offers a Cloud Strategy Kickstart to help accelerate your journey to Azure, or other platforms. Lastly, if you have a question about anything Cloud, Azure or Office 365-related, please don’t hesitate to contact us. We’d love to help you get started.

Share on

Facebook sharing Linkedin sharing button Twitter sharing button

Ready to get started?

Enter your information to keep the conversation going.
Location image
4 Sentry Parkway East, Suite 300, Blue Bell PA, 19422

Email Image

info@anexinet.com

Phono Image610 239 8100


Location Image4 Sentry Parkway East, Suite 300, Blue Bell PA, 19422
Phono Image610 239 8100