Skip to content

Instantly share code, notes, and snippets.

@sandromello
Created August 31, 2015 20:45
Show Gist options
  • Save sandromello/6d5284f3f168d2736f29 to your computer and use it in GitHub Desktop.
Save sandromello/6d5284f3f168d2736f29 to your computer and use it in GitHub Desktop.
Bulk set license to unlicensed users
# Usage: set-plan.ps1 client:licenseplan c:/users.txt
# It will loop for all unlicensed users and check if exists in c:/users.txt file
# In case of a match, it will set the license of the users matched.
# Note: Usage location defaults to BR
param([String]$licensePlan="", [String]$accountsPath="");
import-module MSOnline;
if ([String]::IsNullOrEmpty($licensePlan) -Or [String]::IsNullOrEmpty($accountsPath)){
Write-Host "Empty licensePlan or accountsPath parameter! Exiting...";
Write-Host "Usage set-plan.ps1 contoso:license_plan C:\users.txt";
Exit(1);
}
if (!(Test-Path $accountsPath)){
Write-Host "Cannot find path: "$accountsPath;
Exit(1);
}
# load accounts
$enterpriseAccounts = Get-Content $accountsPath;
$msolcred = Get-Credential;
Connect-MsolService -Credential $msolcred;
Write-Host "Getting unlicensed users...";
$unlicesedUsers = Get-MsolUser -All -UnlicensedUsersOnly;
foreach ($msolUser in $unlicesedUsers){
if ($msolUser.UserPrincipalName.EndsWith("onmicrosoft.com")){
Write-Host "Skipping account " $msolUser.UserPrincipalName;
continue;
}
if ($enterpriseAccounts -contains $msolUser.UserPrincipalName){
Write-Host ("Aplying License {0} to user {1}" -f $licensePlan, $msolUser.UserPrincipalName);
# Set Location, important for defining the license
#Set-MsolUser -UserPrincipalName $msolUser.UserPrincipalName -UsageLocation "BR"
#Set-MsolUserLicense -UserPrincipalName $msolUser.UserPrincipalName -AddLicense $licensePlan;
}
}
Write-Host "Done";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment