Get Office 365 Licenses

Here’s a quick function I put together that prints out a clean looking table of licensing details from Office 365.

In order to use this you will need to replace ‘TenantName’ with the name of your Office 365 tenant name. If you do not know your tenant name you can go here to get it, your tenant name will be the one that ends in .onmicrosoft.com, you will need to have admin access to your account to view that page.

function Get-Office365Licenses {
    <#
.DESCRIPTION
    Developer:  Mike Polselli
    PSVersion:  5.1
    Language:   PowerShell
    Purpose:    Outputs the licenses from Office365, used and unused.
.PARAMETER Help
    Displays helpful information about the script.
.EXAMPLE
    Get-Office365Licenses -Help
#>
    [cmdletbinding(DefaultParameterSetName = "Primary")]
    param (
        [Parameter(ParameterSetName = "Help")][switch]$Help
    )
    if ($Help) {
        Get-Help $MyInvocation.MyCommand.Name -Full | more
        break
    }

    try {
        $MSOnlineCheck = Get-Module -Name MSOnline -ListAvailable
        if (!$MSOnlineCheck) {
            Write-Output 'Microsoft Online Module not installed.'
            Write-Output 'Installing Microsoft Online Module...'
            Install-Module -Name MSOnline -Scope CurrentUser
            Write-Output 'Done.'`
        }
        $Credential = (Get-Credential)
        Connect-MsolService -Credential $Credential
        $Licenses = Get-MsolAccountSku | Sort-Object -Property AccountSkuID
        $RenamedLicenses = @()
        foreach ($License in $Licenses) {
            switch ($License.AccountSkuId) {
                'TenantName:ATP_ENTERPRISE' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:ATP_ENTERPRISE', 'Advanced Threat Protection') }
                'TenantName:EMS' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:EMS', 'Enterprise Mobile Security E3') }
                'TenantName:ENTERPRISEPACK' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:ENTERPRISEPACK', 'Office 365 E3') }
                'TenantName:FLOW_FREE' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:FLOW_FREE', 'Flow') }
                'TenantName:MCOEV' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:MCOEV', 'Phone System') }
                'TenantName:MCOMEETADV' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:MCOMEETADV', 'Audio Conferencing') }
                'TenantName:MCOPSTN1' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:MCOPSTN1', 'Domestic Calling') }
                'TenantName:MEETING_ROOM' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:MEETING_ROOM', 'Meeting Room') }
                'TenantName:MS_TEAMS_IW' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:MS_TEAMS_IW', 'Teams Trial') }
                'TenantName:NONPROFIT_PORTAL' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:NONPROFIT_PORTAL', 'Non-profit Portal') }
                'TenantName:POWER_BI_STANDARD' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:POWER_BI_STANDARD', 'PowerBI Standard') }
                'TenantName:PROJECTPROFESSIONAL' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:PROJECTPROFESSIONAL', 'Project Pro') }
                'TenantName:STANDARDPACK' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:STANDARDPACK', 'Office 365 E1') }
                'TenantName:VISIOCLIENT' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:VISIOCLIENT', 'Visio Pro') }
                'TenantName:WINDOWS_STORE' { $RenamedLicenses += $License.AccountSkuId.Replace('TenantName:WINDOWS_STORE', 'Windows Store') }
            }
        }
    
        $TableName = 'Office365 Licenses'
        $Table = New-Object System.Data.DataTable $TableName
        $ColumnNames = @('License Name', 'Active', 'Total', 'Unused')
        foreach ($Name in $ColumnNames) {
            $Column = New-Object System.Data.DataColumn $Name
            $Table.Columns.Add($Column)
        }
    
        for ($i = 0; $i -lt $RenamedLicenses.Count; $i++) {
            New-Variable -Name Row$i -Force
            $Row = $Table.NewRow()
            $Row.'License Name' = $RenamedLicenses[$i]
            $Row.Active = $Licenses.ConsumedUnits[$i]
            $Row.Total = $Licenses.ActiveUnits[$i]
            $Row.Unused = $Licenses.ActiveUnits[$i] - $Licenses.ConsumedUnits[$i]
            $Table.Rows.Add($Row)
        }
        return $Table
    }
    catch {
        Write-Warning $PSITem.Exception.Message
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s