Skip to content

Instantly share code, notes, and snippets.

@anthonyeden
Last active June 9, 2018 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyeden/e77f08142c16f29f625483f920b6cb52 to your computer and use it in GitHub Desktop.
Save anthonyeden/e77f08142c16f29f625483f920b6cb52 to your computer and use it in GitHub Desktop.
Office 365 Group Licensing
# This script adds Members of the group "Office365Licensing" to the right licenses in Office 365
# It doesn't remove licenses if they are removed from the group - this must be done manually
# Written by Anthony Eden (https://mediarealm.com.au/)
################################################################
# CAUTION: This script contains hard-coded passwords!!!
################################################################
$user = "FULL USERNAME GOES HERE!!!"
$pass = convertto-securestring "FULL PASSWORD GOES HERE!!!" -asplaintext -force
$Credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
$UserCredential = Get-Credential $Credentials
Connect-MsolService -Credential $UserCredential
$GroupID = Get-MsolGroup | Where-Object {$_.DisplayName -eq "Office365Licensing "}
$users = Get-MsolGroupMember -GroupObjectId $GroupID.ObjectId
$SKUs = Get-MsolAccountSku
$SKU = "companyname:STANDARDPACK"
Write-Output $SKUs
$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $SKU -DisabledPlans "YAMMER_ENTERPRISE", "POWERAPPS_O365_P1"
foreach($user in $users) {
$UserData = Get-MsolUser -UserPrincipalName $user.EmailAddress | Select-Object UsageLocation, Licenses
if($UserData.UsageLocation -eq $null) {
Set-MsolUser -UserPrincipalName $user.EmailAddress -UsageLocation "AU"
}
if($UserData.Licenses.AccountSkuID -ne $SKU) {
Write-Output $user.EmailAddress, "Add SKU and Licenses"
Set-MsolUserLicense -UserPrincipalName $user.EmailAddress -AddLicenses $SKU -LicenseOptions $LicenseOptions
} else {
Write-Output $user.EmailAddress, "Add Licenses"
Set-MsolUserLicense -UserPrincipalName $user.EmailAddress -LicenseOptions $LicenseOptions
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment