2023-04-25 13:15:58 +05:30
<#
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Name : Export Office 365 users ' license report
website : o365reports . com
2023-07-17 16:00:56 +05:30
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Script Highlights :
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
1 . The script uses MS Graph PowerShell and installs MS Graph PowerShell SDK ( if not installed already ) upon your confirmation .
2 . It can be executed with certificate-based authentication ( CBA ) too .
3 . Exports Office 365 user license report to CSV file .
4 . You can choose to either “ export license report for all office 365 users ” or pass an input file to get license report of specific users alone .
5 . License Name is shown with its friendly name like ‘ Office 365 Enterprise E3 ’ rather than ‘ ENTERPRISEPACK ’ .
6 . The script can be executed with MFA enabled account too .
7 . The script gives 2 output files . One with the detailed report of O365 Licensed users another with the simple details .
2023-04-25 13:15:58 +05:30
For detailed Script execution : https : / / o365reports . com / 2018 / 12 / 14 / export-office - 365 -user -license -report -powershell /
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
#>
Param
(
[ Parameter ( Mandatory = $false ) ]
[ string ] $UserNamesFile ,
[ string ] $TenantId ,
[ string ] $ClientId ,
[ string ] $CertificateThumbprint
)
Function ConnectMgGraphModule
{
2023-07-17 16:00:56 +05:30
$MsGraphBetaModule = Get-Module Microsoft . Graph . Beta -ListAvailable
if ( $MsGraphBetaModule -eq $null )
2023-04-25 13:15:58 +05:30
{
2023-07-17 16:00:56 +05:30
Write-host " Important: Microsoft Graph Beta module is unavailable. It is mandatory to have this module installed in the system to run the script successfully. "
$confirm = Read-Host Are you sure you want to install Microsoft Graph Beta module ? [ Y] Yes [N ] No
2023-04-25 13:15:58 +05:30
if ( $confirm -match " [yY] " )
{
2023-07-17 16:00:56 +05:30
Write-host " Installing Microsoft Graph Beta module... "
Install-Module Microsoft . Graph . Beta -Scope CurrentUser -AllowClobber
Write-host " Microsoft Graph Beta module is installed in the machine successfully " -ForegroundColor Magenta
2023-04-25 13:15:58 +05:30
}
else
{
2023-07-17 16:00:56 +05:30
Write-host " Exiting. `n Note: Microsoft Graph Beta module must be available in your system to run the script " -ForegroundColor Red
2023-04-25 13:15:58 +05:30
Exit
}
}
if ( ( $TenantId -ne " " ) -and ( $ClientId -ne " " ) -and ( $CertificateThumbprint -ne " " ) )
{
Connect-MgGraph -TenantId $TenantId -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -ErrorAction SilentlyContinue -ErrorVariable ConnectionError | Out-Null
if ( $ConnectionError -ne $null )
{
Write-Host $ConnectionError -Foregroundcolor Red
Exit
}
}
else
{
Connect-MgGraph -Scopes " Directory.Read.All " -ErrorAction SilentlyContinue -Errorvariable ConnectionError | Out-Null
if ( $ConnectionError -ne $null )
{
Write-Host $ConnectionError -Foregroundcolor Red
Exit
}
}
2023-07-17 16:00:56 +05:30
Write-Host " Microsoft Graph Beta PowerShell module is connected successfully " -ForegroundColor Green
2023-04-25 13:15:58 +05:30
}
Function Get_UsersLicenseInfo
{
$LicensePlanWithEnabledService = " "
$FriendlyNameOfLicensePlanWithService = " "
$UPN = $_ . UserPrincipalName
$Country = $_ . Country
if ( $Country -eq " " )
{
$Country = " - "
}
Write-Progress -Activity " `n Exported user count: $LicensedUserCount " ` n " Currently Processing: $upn "
2023-07-17 16:00:56 +05:30
$SKUs = Get-MgBetaUserLicenseDetail -UserId $UPN -ErrorAction SilentlyContinue
2023-04-25 13:15:58 +05:30
$LicenseCount = $SKUs . count
$count = 0
foreach ( $Sku in $SKUs ) #License loop
{
2023-07-17 16:00:56 +05:30
if ( $FriendlyNameHash [ $Sku . SkuPartNumber ] )
2023-04-25 13:15:58 +05:30
{
2023-07-17 16:00:56 +05:30
$NamePrint = $FriendlyNameHash [ $Sku . SkuPartNumber ]
2023-04-25 13:15:58 +05:30
}
else
{
2023-07-17 16:00:56 +05:30
$NamePrint = $Sku . SkuPartNumber
2023-04-25 13:15:58 +05:30
}
#Get all services for current SKUId
$Services = $Sku . ServicePlans
if ( ( $Count -gt 0 ) -and ( $count -lt $LicenseCount ) )
{
$LicensePlanWithEnabledService = $LicensePlanWithEnabledService + " , "
$FriendlyNameOfLicensePlanWithService = $FriendlyNameOfLicensePlanWithService + " , "
}
$DisabledServiceCount = 0
$EnabledServiceCount = 0
$serviceExceptDisabled = " "
$FriendlyNameOfServiceExceptDisabled = " "
foreach ( $Service in $Services ) #Service loop
{
$flag = 0
$ServiceName = $Service . ServicePlanName
if ( $service . ProvisioningStatus -eq " Disabled " )
{
$DisabledServiceCount + +
}
else
{
$EnabledServiceCount + +
if ( $EnabledServiceCount -ne 1 )
{
$serviceExceptDisabled = $serviceExceptDisabled + " , "
}
$serviceExceptDisabled = $serviceExceptDisabled + $ServiceName
$flag = 1
}
#Convert ServiceName to friendly name
2023-07-17 16:00:56 +05:30
$ServiceFriendlyName = $ServiceArray | Where-Object { $_ . Service_Plan_Name -eq $ServiceName }
if ( $ServiceFriendlyName -ne $Null )
{
$ServiceFriendlyName = $ServiceFriendlyName [ 0 ] . ServiceFriendlyNames
}
else
{
$ServiceFriendlyName = $ServiceName
}
2023-04-25 13:15:58 +05:30
if ( $flag -eq 1 )
{
if ( $EnabledServiceCount -ne 1 )
{
$FriendlyNameOfServiceExceptDisabled = $FriendlyNameOfServiceExceptDisabled + " , "
}
$FriendlyNameOfServiceExceptDisabled = $FriendlyNameOfServiceExceptDisabled + $ServiceFriendlyName
}
$Result = [ PSCustomObject ] @ { 'DisplayName' = $_ . Displayname ; 'UserPrinciPalName' = $UPN ; 'LicensePlan' = $Sku . SkuPartNumber ; 'FriendlyNameofLicensePlan' = $nameprint ; 'ServiceName' = $ServiceName ; 'FriendlyNameofServiceName' = $serviceFriendlyName ; 'ProvisioningStatus' = $service . ProvisioningStatus }
$Result | Export-Csv -Path $ExportCSV -Notype -Append
}
if ( $Disabledservicecount -eq 0 )
{
$serviceExceptDisabled = " All services "
$FriendlyNameOfServiceExceptDisabled = " All services "
}
$LicensePlanWithEnabledService = $LicensePlanWithEnabledService + $Sku . SkuPartNumber + " [ " + $serviceExceptDisabled + " ] "
$FriendlyNameOfLicensePlanWithService = $FriendlyNameOfLicensePlanWithService + $NamePrint + " [ " + $FriendlyNameOfServiceExceptDisabled + " ] "
#Increment SKUid count
$count + +
}
$Output = [ PSCustomObject ] @ { 'Displayname' = $_ . Displayname ; 'UserPrincipalName' = $UPN ; Country = $Country ; 'LicensePlanWithEnabledService' = $LicensePlanWithEnabledService ; 'FriendlyNameOfLicensePlanAndEnabledService' = $FriendlyNameOfLicensePlanWithService }
$Output | Export-Csv -path $ExportSimpleCSV -NoTypeInformation -Append
}
function CloseConnection
{
Disconnect-MgGraph | Out-Null
Exit
}
Function main ( )
{
ConnectMgGraphModule
2023-07-17 16:00:56 +05:30
Write-Host " `n Note: If you encounter module related conflicts, run the script in a fresh PowerShell window. " -ForegroundColor Yellow
2023-04-25 13:15:58 +05:30
#Set output file
$ExportCSV = " .\DetailedO365UserLicenseReport_ $( ( Get-Date -format yyyy-MMM -dd -ddd ` hh-mm ` tt ) . ToString ( ) ) .csv "
$ExportSimpleCSV = " .\SimpleO365UserLicenseReport_ $( ( Get-Date -format yyyy-MMM -dd -ddd ` hh-mm ` tt ) . ToString ( ) ) .csv "
#FriendlyName list for license plan and service
try {
2023-07-17 16:00:56 +05:30
$FriendlyNameHash = Get-Content -Raw -Path . \ LicenseFriendlyName . txt -ErrorAction SilentlyContinue -ErrorVariable ERR | ConvertFrom-StringData
if ( $ERR -ne $null )
{
Write-Host $ERR -ForegroundColor Red
CloseConnection
}
$ServiceArray = Import-Csv -Path . \ ServiceFriendlyName . csv
2023-04-25 13:15:58 +05:30
}
catch
{
Write-Host $_ . Exception . message -ForegroundColor Red
CloseConnection
}
#Get licensed user
$LicensedUserCount = 0
#Check for input file/Get users from input file
if ( $UserNamesFile -ne " " )
{
#We have an input file, read it into memory
$UserNames = @ ( )
$UserNames = Import-Csv -Header " UserPrincipalName " $UserNamesFile
foreach ( $item in $UserNames )
{
2023-07-17 16:00:56 +05:30
Get-MgBetaUser -UserId $item . UserPrincipalName -ErrorAction SilentlyContinue | Where-Object { $_ . AssignedLicenses -ne $null } | ForEach-Object {
Get_UsersLicenseInfo
$LicensedUserCount + +
2023-04-25 13:15:58 +05:30
}
}
}
#Get all licensed users
else
{
2023-07-17 16:00:56 +05:30
Get-MgBetaUser -All | Where-Object { $_ . AssignedLicenses -ne $null } | ForEach-Object {
Get_UsersLicenseInfo
$LicensedUserCount + +
}
2023-04-25 13:15:58 +05:30
}
#Open output file after execution
if ( ( Test-Path -Path $ExportCSV ) -eq " True " )
2023-07-17 16:00:56 +05:30
{ Write-Host ` n " Detailed report available in: " -NoNewline -ForegroundColor Yellow ; Write-Host " $ExportCSV "
Write-Host ` n " Simple report available in: " -NoNewline -ForegroundColor Yellow ; Write-Host " $ExportSimpleCSV " ` n
2023-04-25 13:15:58 +05:30
$Prompt = New-Object -ComObject wscript . shell
$UserInput = $Prompt . popup ( " Do you want to open output files? " , ` 0 , " Open Files " , 4 )
if ( $UserInput -eq 6 )
{
Invoke-Item $ExportCSV
Invoke-Item $ExportSimpleCSV
}
}
else
{
2023-07-17 16:00:56 +05:30
Write-Host " No data found "
2023-04-25 13:15:58 +05:30
}
2023-07-17 16:00:56 +05:30
Write-Host ` n ~ ~ Script prepared by AdminDroid Community ~ ~ ` n -ForegroundColor Green
Write-Host " ~~ Check out " -NoNewline -ForegroundColor Green ; Write-Host " admindroid.com " -ForegroundColor Yellow -NoNewline ; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~ " -ForegroundColor Green ` n ` n
2023-04-25 13:15:58 +05:30
CloseConnection
}
. main