Assign O365 License based on OU attribute

I'm looking for PS that will first evaluate the user's OU attribute, and if in OU=Test, assign license (tenantname:AccountSkuID_1), but if in any other OU, remove it and assign a different license. (tenantname:AccountSkuID_2)

Please assist.

thank you!
Anthony K O365Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
Just list the users in said OU via

$users = Get-ADUser -SearchBase "OU=blabla,DC=blah,DC=com" -Filter * | select UserPrincipalName

Open in new window

Then license them however you like. For example:

$users | % { Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation US; Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses "contoso:ENTERPRISEPACK" }

Open in new window

Removing existing licenses will be a bit tricker as you have to check which licenses they have first, so something like:

$users | % { $license = (Get-MsolUser -UserPrincipalName $_.UserPrincipalName).licenses ;
foreach ($l in $license) { Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses $l.AccountSkuId }

Open in new window

And you can of course add checks to make sure it's the exact license, etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony K O365Author Commented:
How would I simply remove 'tenant:faculty3' if that is the only license I'm looking for?
Vasil Michev (MVP)Commented:
If you are only looking to remove a single license, you can just to it the 'ugly' way:

$users | % { Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "tenant:faculty3" }

Open in new window

It will give you errors for any user that does not have the license applied, which you can address with proper error checking. If you are swaping licenses, take a look at this script:
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Anthony K O365Author Commented:
If you add the license in step 2, wouldn't you be removing the license in step 3?
Vasil Michev (MVP)Commented:
Those are just an examples, make sure to edit them accordingly...
Anthony K O365Author Commented:
Truly appreciate the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.