Export Office 365 Licenses to CSV

$Creds = Get-Credential -Message “Please enter Office 365 Global Admin Credentials”
$InputFile = “C:\temp\UserInput.csv”
$OutputFile = “C:\temp\UserOutput.csv”

Connect-MsolService -Credential $Creds

#$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Creds -Authentication Basic -AllowRedirection
#Import-PSSession $Session

$AllUsersData = import-csv -Path $InputFile

if($AllUsersData)
{
  $UsersInfo = @()
  foreach($aUser in $AllUsersData)
  {
    $DomainName = “@sharepointmvp.onmicrosoft.com”
    $UserPrincipalName = $aUser.Identity + $DomainName
    Write-Host “Checking Licenses for: $UserPrincipalName” -ForeColor Yellow
    $msolUser = Get-MsolUser -UserPrincipalName $UserPrincipalName -ErrorAction SilentlyContinue
    if($msolUser)
    {
      $Licenses = “None”
      if($msolUser.IsLicensed)
      {
        Write-Host “User $UserPrincipalName is Licensed:” -ForegroundColor Green
        $MSOLUserLicense = $msolUser.Licenses.AccountSkuid
        $Licenses = “”
        for($i = 0; $i -lt $MSOLUserLicense.Count; $i++)
        {
          $Licenses += $msolUser.Licenses[$i].AccountSkuId
          if($i -le $MSOLUserLicense.Count)
          {
            $Licenses += “;`n”
          }
        }
      }
     
      $userObj = New-Object PSObject
      Add-Member -input $userObj noteproperty ‘UserPrincipalName’ $UserPrincipalName
      Add-Member -input $userObj noteproperty ‘Alias’ $aUser.Alias
      Add-Member -input $userObj noteproperty ‘DisplayName’ $aUser.DisplayName
      Add-Member -input $userObj noteproperty ‘Licenses’ $Licenses
      $UsersInfo += $userObj
    }
  }
  $UsersInfo | Export-Csv -Path $OutputFile -NoTypeInformation -Force -Verbose
  Write-host “Output written to $OutputFile”
}
start $OutputFile

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