How to create Office 365 Users using PowerShell
In this post, I will look at how we can create new Office 365 Users using PowerShell and look at how we can create bulk Office 365 users using a PowerShell script.
To get started we need to connect to Office 365 using PowerShell. This can be done using the following method.
1 |
$UserCredential = Get-Credential |
Once the Credentials has been specified run the following cmdlet.
1 |
Connect-MsolService -Credential $UserCredential |
Now that we have the connection established, let’s go ahead and create a single user by running the following cmdlet.
The following example will create the New account and assign an E3 license to the user.
1 2 3 |
New-MsolUser -DisplayName "Jason Born" -FirstName Jason -LastName Born -UserPrincipalName Jason.Born@ThatLazyAdmin.com -UsageLocation ZA -LicenseAssignment urbannerdconsulting:ENTERPRISEPACK |
The user account will be created with an auto generated password and will be listed as below.
Now that we have an idea of how to create one user using PowerShell, let’s go ahead and create multiple user using PowerShell with a CSV file.
To get started we first need to create our CSV file, the following headings will be used for the CSV.
Now let’s complete the CSV by adding some users.
Now let’s look at what the PowerShell Script will look like.
For all the users, I will not specify any password and will leave this to Office 365 to auto generate the passwords. In the script, the Export-CSV has been specified as well so we can review the results after the script has completed.
1 2 3 4 |
Import-Csv -Path "C:\Softlib\O365NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\Softlib\O365\NewAccountResults.csv" |
Let’s verify the users we just created now by running the following.
1 |
Get-MsolUser |Where-Object {$_.WhenCreated –Match “11/15/2017”} |
Note: I have added the Where-Object just so that I can see the accounts which I have created during this script.
If you reference the Output File you will get all the passwords which was auto generated during the creation process.
The Script and CSV file can be downloaded here. O365
#ThatLazyAdmin
Be First to Comment