Office 365 Batch Scripts Help

We have exported all the users out of 365 via connecting to the tenancy using the ‘Windows Azure Active Directory Module for Windows PowerShell’ and the command ‘Get-MsolUser -EnabledFilter EnabledOnly -all | Export-csv allusers.csv’ this then gives me a list of users to run commands on. I then sort the csv’s so I have one for students and staff containing a column called upn which has all the account names I wish to apply a command to.

We need help / validation with formulating the commands to assign licenses on bulk to groups of users, We believe we have this one sorted for students but it would be good if someone could validate:

$AccountSkuId = "ashfordschool:OFFICESUBSCRIPTION_STUDENT"

$UsageLocation = "GB"

$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId

$Users = Import-Csv c:\students.csv

$Users | ForEach-Object {

Set-MsolUser -UserPrincipalName $_.upn -UsageLocation $UsageLocation

Set-MsolUserLicense -UserPrincipalName $_.upn -AddLicenses $AccountSkuId


Open in new window

We have two licenses I need to assign to students, one called ‘Office 365 ProPlus for Students’ so the students can get office on their iPads and another coming I believe the A2 plan so they can get mailboxes. I assume if I just run the command twice on the student accounts under the two different license names they will be assigned the two licenses?

 Also for staff I have manually assigned my account the domain and the correct staff license ‘Office 365 Education A3 for Faculty’, however when I go to sign I get a ‘Something Went Wrong’ message (see picture somethingwentwrong.jpg). I took the option ‘Exchange Online (Plan 2)’ part out of the license and could sign in OK (I guess this is because we haven’t migrated our mailboxes yet and won’t be until the next available downtime window). So my question is - I believe when assigning licenses you can assign options of which elements you want. This seems to be a little more complicated than my license script above, so we could do with some assistance with the options.

We need help with formulating the commands to assign domains to users:

We have two domains for staff and for students. I need to change the domain name for these accounts from the default I have the following command but it wouldn’t quite work:

$Users = Import-Csv c:\students.csv

$Users | ForEach-Object { Set-MsolUserPrincipalName -ObjectId $_.upn -NewUserPrincipalName ($_.UserPrincipalName.Split(“@”)[0] + “”)}


Open in new window

Can’t remember the exact error.

Also, we need help with formulating the commands to assign mailboxes to one of the two domains

Need to setup mailboxes just for the students on the have no idea how to do this yet (the Staff are staying on our company exchange box for the time being).
Who is Participating?
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:
For the UPN (and any other attribute for that matter), use the corresponding attribute in your AD. Make sure to add the domain suffixes and change the UPN there. If for whatever reason doing this in the local AD is not a good idea, you can certainly change it in the WAAD, the following should work:

$Users = Import-Csv c:\students.csv

$Users | % { Set-MsolUserPrincipalName -ObjectId $_.objectId -NewUserPrincipalName $($_.UserPrincipalName.Split("@")[0] + "")}


Open in new window

For the OWA error - if your account has a mailbox on-prem, and you are using Dirsync, O365 effectively 'knows' that and will provision it as mail-enabled user. Thus, even if you grant an Exchange license to it, it will not provision new mailbox for it. Note: you cannot manually convert mail-enabled users in EO, the only way to do this is by running the (Staged or Hybrid) migration process.

For the licenses part, first make sure you have the correct SKU:


Open in new window

You do not need to use LicenseOptions unless you want to remove some of the services (i.e. you are assigning the full E3 pack, but you dont want the user to be able to use Lync Online). The cmdlet you need in general is:

$disabledPlans += "MCOSTANDARD"
$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId -DisabledPlans $disabledPlans
Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions

Open in new window

I'm not sure what exactly the EDU plans offer, so you might not need to remove anything, i.e. you can run only the last cmdlet, without even specifying the -LicenseOptions part.

More info on this here:

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
HypervizorAuthor Commented:
Thanks Vasil.
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
Office 365

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.