title: O365 PowerShell Pull SubLicense
body: <p>I am trying to pull a list of all users in my O365 Tenant and if they are licensed, the list of sublicenses they have been granted.  The following code works great to list out my sublicenses:</p>
  
  <pre><code>$userlicensetest = get-msoluser -userprincipalname "steve.dorr@merrillcorp.com"
  $userlicensetest.licenses[0].servicestatus
  </code></pre>
  
  <p>ServicePlan :: ProvisioningStatus</p>
  
  <p>----------- :: ------------------</p>
  
  <p>INTUNE_O365 ::    PendingActivation</p>
  
  <p>YAMMER_ENTERPRISE  :: PendingInput</p>
  
  <p>OFFICESUBSCRIPTION  ::  Success</p>
  
  <p>So I tried to modify code I found online to include the sublicense information.  Here is what I have built so far:</p>
  
  <pre><code>$ReportPath = "c:\users\userlist.csv"
  Add-Content -value ("UserPrincipalName"+","+"IsLicensed"+","+ "Licenses”"+","+ "SubLicenses") -Path $ReportPath
  $AllUsers = Get-MsolUser -All 
  foreach ($User in $AllUsers)
  {
   $UserPrincipalName = $User.UserPrincipalName
   $IsLicensed = $User.IsLicensed
   $Licenses = $User.Licenses.AccountSkuId
   $SubLicenses = $User.Licenses[0].servicestatus
  
   Add-Content -value ($UserPrincipalName+","+$IsLicensed+","+$Licenses+","+$SubLicenses) -Path $ReportPath
  }
  </code></pre>
  
  <p>The problem is it is only pulling the header line from the sublicense query and not all the lines of detail.  So the line for myself in the CSV looks like:</p>
  
  <p>Steve.Dorr@MerrillCorp.com  TRUE    mymerrillcorp:ENTERPRISEPACK    Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus</p>
  
  <p>Which does not give me the detail lines I needed. </p>
  
  <p>How do I pull all the lines that Licenses[0].servicestatus generates into the CSV file?  I don't care whether it flattens it out and goes across more columns, or takes up multiple lines in Excel.</p>
  
  <p>Thanks.</p>
  
  <p>So since I posted this question I have worked a little on it.  I do not have a perfect solution that puts this into a nice neat CSV file, but I do have a routine which now drops all this information into a text file.  Below is my code.</p>
  
  <pre><code>$MyCredentials = Get-Credential -Message "Enter Office 365 Email &amp; Password"
  Connect-MsolService -Credential $MyCredentials
  $ReportFile = "C:\temp\O365Data.txt"
  " " | Out-File $ReportFile  #erases the file if it exists
  
  $AllUsers = Get-MsolUser -All 
  foreach ($User in $AllUsers)
  {
   $UserPrincipalName = $User.UserPrincipalName
   $IsLicensed = $User.IsLicensed
   $Licenses = $User.Licenses.AccountSkuId
   $SubLicenses = $User.Licenses[0].servicestatus
   $OneLine = $UserPrincipalName + " " + $IsLicensed
   $OneLine| Out-File $ReportFile -Append
   if($User.Licenses[0].servicestatus) {$User.Licenses[0].servicestatus | Out-File $ReportFile -Append}
  }
  </code></pre>