Office 365 Batch Scripts Help

Posted on 2014-09-02
Last Modified: 2014-11-12
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).
Question by:Hypervizor
    LVL 37

    Accepted Solution

    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:

    Author Comment

    Thanks Vasil.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Want to promote your upcoming event?

    Is your company attending an event or exhibiting at a trade show soon? Are you speaking at a conference? Spread the word by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

    Companies keep a much closer eye on costs today, so changing to new Technology – Microsoft Office 365 is the smartest move to take.
    Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
    This Experts Exchange lesson shows how to use VBA to loop through rows in Excel.  In order to sort, filter, and use database features, there needs to be a value in each column for every row. When data arrives with values missing, code to copy values…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now