Powershell to create AD user account and assign an Office 365 license with Exchange Online

Eaven HUANG 2,126 Reputation points
2020-11-24T08:38:07.423+00:00

Dear IT pros,

I'm looking for an automation way by using PowerShell to do this task in batch, the requirement would be -
Create AD user account and assign an Office 365 license in Exchange Online without manually creating them one-by-one via GUIs

Suppose we have over 100 new users (that maybe coming as guest for one month or two only), can we use PowerShell to run scripts and maybe read the attributes from a .csv?
Thanks very much in advance for any suggestions and opinion.

Best regards
Eaven

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,173 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. KyleXu-MSFT 26,206 Reputation points
    2020-11-25T03:04:22.083+00:00

    @EavenHuang-0590

    Step 1 : Prepare CSV file as picture below:
    42472-qa-kyle-10-48-42.png

    Step 2: Modify the script below:

    $Mailboxes  = import-csv c:/temp/users.csv #Location of CSV file  
    $PassWord = "Password123" #Default password for new created users, they are forced to change their password the next time they sign in.  
    $planName="domain:SPE_E3" #Plane name of your Office 365. "Get-MsolAccountSku"  
      
    foreach($Mailbox in $Mailboxes){  
        New-MsolUser -UserPrincipalName $Mailbox.PrimarySmtpAddress -DisplayName $Mailbox.DisplayName -UsageLocation us -Password $password -ForceChangePassword $true  
        Set-MsolUserLicense -UserPrincipalName $Mailbox.PrimarySmtpAddress -AddLicenses $planName  
    }  
    

    Step 3: Connect with the Microsoft Azure Active Directory Module for Windows PowerShell
    Step 4: Run this script(The output is the result of creating users, and those users will be assigned license in the next step automatically):

    42483-qa-kyle-10-57-49.png

    You can use command below to double check the license status of new users:
    42501-qa-kyle-10-59-20.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.