Sulich's Blog

Sulich's Blog

Cool script for getting all individual license plans for all licensed users in Office 365

https://community.spiceworks.com/scripts/show/3698-report-on-office-365-user-license-assignment-with-powershell

by

Description

Get-O365UserLicenseReport is a Windows PowerShell function that outputs Office 365 user license assignments, including individual service plans, that can be easily viewed or exported to file.

function Get-O365UserLicenseReport
{
<#
.SYNOPSIS
Generates a report of Office 365 license assignments
.DESCRIPTION
Generates a report of Office 365 license assignments by license sku, including individual service plan assignments.

This command requires that you have a global administrator account for an Office 365 tenant and can connect to Office 365 using
Windows PowerShell. Instructions for this can be found at http://powershell.office.com/.

.PARAMETER MsolUser
The Office 365 user account(s) that you’d like to report on.

.PARAMETER AccountSkuId
The name of the license sku you’d like to report on. These can be found by running the command:

Get-MsolAccountSkuId

.PARAMETER UserProperties
Optional. Any Office 365 user properties that you’d like to include in the report. These could help you determine a user’s role to aid in finding
where license assignments are incorrect. For instance, if your users are provided certain license assignments based on their department
you may want to include the “Department” property in the resulting report for comparison.

Acceptable values are:

City
Country
Department
FirstName
LastName
Office
PhoneNumber
PostalCode
State
StreetAddress
Title
UsageLocation

The DisplayName and UserPrincipalName will always be included, regardless of properties specified by the UserProperties parameter.

.EXAMPLE
$Credential = Get-Credential
Connect-MsolService -Credential $Credential
Get-MsolUser -All | Where IsLicensed -eq $true |
Get-O365UserLicenseReport -AccountSkuId whitehouse:Enterprisepack -UserProperties Department,Office

This example will return information for all your Office 365 users who are assigned at least one service plan in the EnterprisePack
license sku. It starts by saving a credential object in the $Credential variable. Next you collect all your Office 365 users with at
least one license and pass those down the pipeline to the Get-O365UserLicenseReport command.

.EXAMPLE
Get-MsolUser -All | Where IsLicensed -eq $true |
Get-O365UserLicenseReport -AccountSkuId whitehouse:Enterprisepack -UserProperties Department,Office |
Export-Csv c:\Reports\EnterprisePack.csv -NoTypeInformation

This example, like the previous one, will collect license information for all your users, but this time will export it to a csv file for
posterity.

.EXAMPLE
$Skus = Get-MsolAccountSku | Where-Object { $_.ConsumedUnits -ge 1 }
$Users = Get-MsolUser -All | Where-Object { $_.IsLicensed -eq $true }

foreach ($Sku in $Skus)
{
Get-O365UserLicenseReport -MsolUser $Users -AccountSkuId $Sku.AccountSkuId -UserProperties Department,Office |
Export-Csv “c:\Reports\$($Sku.SkuPartNumber).csv” -NoTypeInformation
}

This example is what I call the nuclear option – it will collect information for all your license skus that have at least one assigned
user and output a report for each in CSV format. These will be saved to the folder location “c:\Reports” and each file name will be match
the sku name.
#>

[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.Online.Administration.User[]]
$MsolUser,

[Parameter(Mandatory = $true)]
[string]
$AccountSkuId,

[Parameter(Mandatory = $false)]
[ValidateSet(
“City”,
“Country”,
“Department”,
“FirstName”,
“LastName”,
“Office”,
“PhoneNumber”,
“PostalCode”,
“State”,
“StreetAddress”,
“Title”,
“UsageLocation”
)]
[string[]]
$UserProperties
)

process
{
foreach ($User in $MsolUser)
{
$License = $User.Licenses | Where-Object { $_.AccountSkuId -eq $AccountSkuId }

if ($License)
{
$Properties = [Ordered]@{
DisplayName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
}

foreach ($Property in $UserProperties)
{
$Properties.Add($Property, $User.$Property)
}

foreach ($Plan in $License.ServiceStatus)
{
$Properties.Add($Plan.ServicePlan.ServiceName, $Plan.ProvisioningStatus)
}

New-Object -TypeName PSCustomObject -Property $Properties
}
}
}
}

Example for use :

$Skus = Get-MsolAccountSku | Where-Object { $_.ConsumedUnits -ge 1 }
$Users = Get-MsolUser -All | Where-Object { $_.IsLicensed -eq $true }

foreach ($Sku in $Skus)
{
Get-O365UserLicenseReport -MsolUser $Users -AccountSkuId $Sku.AccountSkuId -UserProperties Department,Office |
Export-Csv “c:\Reports\$($Sku.SkuPartNumber).csv” -NoTypeInformation
}

Single Post Navigation

Leave a comment