10 useful Office 365 PowerShell snippets

Using the Windows Azure Active Directory Module connect to
Azure AD using the following command.

 

Connect-MsolService

 

Enter your tenant credentials. (user@contoso.onmicrosoft.com)

 

1.Export to CSV all users that are licensed and have a specific UPN domain prefix.

 

Get-MsolUser -all |where {$_.isLicensed -And $_.UserPrincipalName.ToLower().EndsWith("contoso.com")} | Export-csv userTrue.csv

 

2.Export to CSV all users that are NOT licensed and have a specific UPN domain prefix.

 

Get-MsolUser -all |where {!$_.isLicensed -And $_.UserPrincipalName.ToLower().EndsWith("contoso.com")} | Export-csv userFalse.csv

 

3.Get the Account Skus associated with a tenant (e.g License Pack(s) used to assign a license to a user)

Get-MsolAccountSku | ft accountskuid

Returns

AccountSkuId

------------

Contoso:ENTERPRISEPACK_B_PILOT

4.Report the service names associated with a particular SKU (e,g Exchange, Lync, Sharepoint, Yammer, etc)

$a = get-MsolAccountSku | where{$_.AccountSkuID -eq "Contoso:ENTERPRISEPACK_B_PILOT"}

$a.servicestatus

Returns

ServicePlan      
                               
              
ProvisioningStatus

-----------------------------

RMS_S_ENTERPRISE_B_PILOT                              Success

OFFICE_PRO_PLUS_SUBSCRIPTION_B_PILOT    Success

LYNC_S_ENTERPRISE_B_PILOT                            Success

SHAREPOINTWAC_B_PILOT                                   Success

SHAREPOINT_S_ENTERPRISE_B_PILOT              Success

EXCHANGE_S_ENTERPRISE_B_PILOT                 Success

 

5. Assign a License SKU to a particular user.

 

Set-MSOLUser -UserPrincipalName " user@contoso.com " -UsageLocation GB

Set-MsolUserLicense -UserPrincipalName " user@contoso.com " -AddLicenses Contoso:ENTERPRISEPACK_B_PILOT

 

6. Assign an element(s) of License SKU to a particular user (e.g Disable Lync).

 

$DisableComponents = New-MsolLicenseOptions -AccountSkuId Contoso:ENTERPRISEPACK_B_PILOT -DisabledPlans LYNC_S_ENTERPRISE_B_PILOT

Set-MSOLUser -UserPrincipalName " user@contoso.com " -UsageLocation GB

Set-MsolUserLicense -UserPrincipalName " user@contoso.com " -AddLicenses Contoso:ENTERPRISEPACK_B_PILOT -LicenseOptions $DisableComponents

7. Get the SKU Associated to a particular user (i.e. what license pack has been assigned)

$a = Get-MsolUser -UserPrincipalName " user@contoso.com "

$a.licenses.accountskuid

 

Returns

 

Contoso:ENTERPRISEPACK_B_PILOT

 

8.Get the provisioning status of each of the service names for a user. (i.e is Exchange enabled, Lync disabled etc)

 

$a = Get-MsolUser -UserPrincipalName " user@contoso.com "

$a.licenses.servicestatus

 

Returns

ServicePlan                            
                                       
ProvisioningStatus

-----------------------------

RMS_S_ENTERPRISE_B_PILOT                             Success

OFFICE_PRO_PLUS_SUBSCRIPTION_B_PILOT   Success

LYNC_S_ENTERPRISE_B_PILOT                            Disabled

SHAREPOINTWAC_B_PILOT                                   Success

SHAREPOINT_S_ENTERPRISE_B_PILOT              Success

EXCHANGE_S_ENTERPRISE_B_PILOT                 Success

 

9. Delete a user from the tenant and the recycle bin (managed identities)

Remove-MsolUser -UserPrincipalName “ user@contoso.com ” -Force

Remove-MsolUser -UserPrincipalName “ user@contoso.com ” -RemoveFromRecycleBin

 

 10. Convert the BASE64 GUID from a Azure AD user back to an AD GUID. (Example of usage would be a PS Script to write an attribute to AD for each user in Office365 – AD GUID provides the bind to user in AD)

$a = Get-MsolUser -UserPrincipalName " user@contoso.com "

$GUID = new-Object -TypeName System.Guid -ArgumentList(,( ([System.Convert]::FromBase64String($a.ImmutableId)) ) )

$getAD = Get-ADUser -Identity $GUID

Comments

  • Anonymous
    March 20, 2015
    Very useful indeed!!

  • Anonymous
    May 05, 2015
    Excellent, great, help me a lot  

  • Anonymous
    May 05, 2015
    Glad its of use!

  • Anonymous
    June 23, 2017
    Useful information.